# Julia equivalent of the IDL WHERE() function?

**URL:** <https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594>\
**Category:** New to Julia\
**Created:** [August 27, 2017, 4:04pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594 "2017-08-27T16:04:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [August 27, 2017, 4:04pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/1 "2017-08-27T16:04:21Z")

</div>

Having programmed mostly in IDL before coming to Julia, is there a command similar to IDL’s WHERE() function? The WHERE() function returns an array of indeces given a certain condition, so in IDL I could write:

```julia
> day = ["mon","tue","wed","mon"]
> index = where(day eq "mon" or day eq "tue")
> print,day[index]
mon tue mon

```

I just don’t know the best way to achieve a similar result with Julia.

Thanks in advance for your help!  
Michael

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [August 27, 2017, 4:58pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/2 "2017-08-27T16:58:29Z")

</div>

This works:

```julia
julia> day = ["mon","tue","wed","mon"]
4-element Array{String,1}:
 "mon"
 "tue"
 "wed"
 "mon"

julia> idx = find(x -> x in ["mon", "tue"], day)
3-element Array{Int64,1}:
 1
 2
 4

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [August 27, 2017, 5:10pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/3 "2017-08-27T17:10:43Z")

</div>

Hi Michael,  
I can give you a straight forward version using -\>dots

```julia
day = ["mon","tue","wed","mon"]
index = (day .== "mon") .| (day .== "tue")
day[index]

```

```julia
3-element Array{String,1}:
 "mon"
 "tue"
 "mon"

```

and a “modern” solution using -\>generators with if clause

```julia
collect(d for d in day if d == "mon" || d == "tue")

```

Best

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [August 27, 2017, 9:50pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/4 "2017-08-27T21:50:24Z")

</div>

Thanks for the quick responses. I will test out the choices in my code and see which one makes the most sense to use.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [August 27, 2017, 9:54pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/5 "2017-08-27T21:54:30Z")

</div>

I tested out the codes you supplied, and the dots version worked, but the version with collect() gave me a synatx error, “missing comma or ) in argument list”. I am using Julia version 0.4.5 (supplied by Ubuntu repos). I would probably use the dots the version anyway, but would the error be related to the julia version I am using?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 28, 2017, 2:30am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/6 "2017-08-28T02:30:48Z")

</div>

Yes, the collect generator version should work with `v0.5` or higher.

And here’s yet another alternative:

```julia
filter(d -> d == "mon" || d == "tue", day)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 28, 2017, 5:49am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/7 "2017-08-28T05:49:18Z")

</div>

I would really recommend to get a more up to date version than 0.4. Tremendous improvements have been done on the language since then. You can just download it from [Download Julia](https://julialang.org/downloads/), extract it and run the executable in the `bin` folder.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 28, 2017, 6:25am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/8 "2017-08-28T06:25:32Z")

</div>

@mihalybaci please follow @kristoffer.carlsson’s advise and get a decently recent version of Julia.

---

<div class="post-metadata">

**Author:** ![jmgnve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmgnve/32/1906_2.png) [@jmgnve](https://discourse.julialang.org/u/jmgnve)\
**Post date:** [August 28, 2017, 6:33am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/9 "2017-08-28T06:33:49Z")

</div>

Yet another option:

```julia
julia> day = ["mon","tue","wed","mon"]
julia> ind = findin(day, ["mon", "tue"])
julia> day[ind]
3-element Array{String,1}:
 "mon"
 "tue"
 "mon"

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 28, 2017, 8:53am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/10 "2017-08-28T08:53:48Z")

</div>

I would say that `find` or `findin` are the most correct answers to this question _as written_. They are equivalent to the `WHERE` function in that they return an array of indices.

The other approaches (`filter`, generator, and the logical index) don’t do that. But perhaps the what the OP really wanted was finding the elements themselves, and only wanted `WHERE` as an intermediate step?

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [August 28, 2017, 11:07am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/11 "2017-08-28T11:07:51Z")

</div>

I have thought about it, but have stuck with the Ubuntu 16.04 repos just to make sure everything works, but I will test out the latest stable binary,

As for my intent, I do need an array of indices so that I can feed it into another array to pick out subsets, and the original post is really a simple version of what I’d like to do in the future. For instance, if I have a catalog of star positions (x,y) and I wanted to pick out all the sources within a circlular area given by (x\_center,y\_center,radius) I could write this

```julia
> c = where(sqrt((x - x_center))^2. + (y - y_center)^2.) le radius/60.)
> print,star_names[c]
Betelgeuese

```

where x and y are separate arrays and “le” is less than or equal to.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 28, 2017, 11:14am UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/12 "2017-08-28T11:14:45Z")

</div>

Yeah, I completely understand that using whatever the distro supplies is the most convenient way. I would argue that there is a failure from our side that there is such an outdated PPA available. It should either not be there at all, or be updated in a timely manner with new Julia versions.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 28, 2017, 3:17pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/13 "2017-08-28T15:17:31Z")

</div>

@mihalybaci, given the number of alternative solutions you have, what about creating an answer where you collect all the alternatives? It is great to see so many clean solutions for the same task, putting them all together would facilitate comparison and show the syntax strengths for new comers. 🙂

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [August 28, 2017, 5:15pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-the-idl-where-function/5594/14 "2017-08-28T17:15:02Z")

</div>

@juliohm, I was considering running a few tests to see if there were any appreciable speed differences between the methods for what I intend to do. If I get around to it, I could add the results here.
