# Finding Position of Element in an Array

**URL:** https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502
**Category:** General Usage
**Tags:** question
**Created:** [July 19, 2018, 4:25pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502 "2018-07-19T16:25:22Z")
**Posts on this page:** 7
**Page:** 3

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [October 9, 2019, 9:40am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/41 "2019-10-09T09:40:27Z")

</div>

I do have to account for `nothing` so I have some crazy code which looks like this:

```julia
first(something(findfirst("multipart/form-data", get(headers, "Content-Type", "")), 0:-1)) != 0

```

It looks like returning `-1` would make the check simpler. Although I do agree that it’s inconsistent with the way Julia represents nullable values (as a `Union{Something,Nothing}`).

---

<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: [October 9, 2019, 9:47am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/42 "2019-10-09T09:47:17Z")

</div>

> [@essenciary](#):
>
> I do have to account for `nothing`

In what? When will `nothing` be a valid _key_ (not value)?

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [October 9, 2019, 10:45am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/43 "2019-10-09T10:45:55Z")

</div>

Sorry, I think I’m missing your point. Probably my fault as I frankly can’t compute that code in my head. It’s an actual example of a complex sequence of `find*` and `something`. If I’m using them wrong and the code can be refactored, my apologies. Otherwise, I wish there was a simpler way, that snippet is painful to reason about.

---

<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: [October 9, 2019, 11:37am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/44 "2019-10-09T11:37:38Z")

</div>

Is the following what you are trying to do?

```julia
julia> headers = Dict("Content-Type" => "multipart/form-data; boundary=something")
Dict{String,String} with 1 entry:
  "Content-Type" => "multipart/form-data; boundary=something"

julia> first(something(findfirst("multipart/form-data", get(headers, "Content-Type", "")), 0:-1)) != 0
true

julia> occursin("multipart/form-data", get(headers, "Content-Type", ""))
true

```

---

<div class="post-metadata">

### Author: ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)
#### Post date: [October 9, 2019, 12:56pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/45 "2019-10-09T12:56:46Z")

</div>

As far as I can tell, the solution @kristoffer.carlsson is referring to and what you are looking for works like this:

```julia
res = findfirst("multipart/form-data", get(headers, "Content-Type", ""))
if !isnothing(res) #alternatively use !==nothing
  println("Found at $res !!")
else
  println("Darn, no such field...")
end

```

`find*` returns `Union{Nothing,T}` and as long as `nothing` is not a valid value for `T` you can simply check for it as in the example I gave, to find out if an occurence was found.  
If `nothing` is a valid value for `T` on the other hand, then you need to encapsulate the values of `T` (like with `something(.)`) and thus make the new type again distinguishable from `Nothing`.

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [October 9, 2019, 1:03pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/46 "2019-10-09T13:03:33Z")

</div>

Oh, that’s pretty 😍

I refactored the logic from

```julia
searchindex(get(headers, "Content-Type", ""), "application/x-www-form-urlencoded") != 0

```

when `searchindex` was deprecated and ended up with that monstrosity.

---

<div class="post-metadata">

### Author: ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)
#### Post date: [October 9, 2019, 1:10pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/47 "2019-10-09T13:10:31Z")

</div>

that now just became `firstindex(...) !== nothing` 😃

The whole logic now works in fact like the iteration protocol in that it uses the relevant type when found and `nothing` when not found/at end. This works that smooth thanks to some optimizations in the compiler. And the “new” iteration protocol is very smooth in my opinion ^^

[Previous page](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502.md?page=2)
