# Find in an array of custom structs if we have an element with a specific field value or not

Dear All,

Is there a neat way to find whether, in an array of custom structs, we have an entry with a specific field value or not (outputs as `true` or `false`)?

For example, consider the following example. I have a custom `struct` defined as follows:

``````struct MyStruct
i::Int64 # corresponds to index i ∈ [-1:3]
j::Int64 # corresponds to index j ∈ [-1:3]
k::Int64 # corresponds to index k ∈ [0:3]
end
``````

And I have the following array of this custom struct `MyStruct`.

``````A = MyStruct[
MyStruct(0, 2, 0),
MyStruct(2, 0, 0),
MyStruct(2, 3, 0),
MyStruct(0, 2, 1),
MyStruct(2, 0, 1),
MyStruct(2, 3, 1),
MyStruct(0, 2, 2),
MyStruct(2, 0, 2),
MyStruct(2, 3, 2)]
``````

As we can see, for any element of the form `MyStruct(i,j,k)` of `A`, `MyStruct(i,j,k).k` never takes the value `3`, but takes the value `0`,`1`, and `2`. Is there a way to do this programmatically for large arrays of `MyStruct`s, whether in the array of custom structs a specific field value (such as `k == 3`) appears or not (outputs as `true` or `false`) in the entries?

`findfirst` returns Nothing if it finds nothing or the first index.
To find all indices: `findall`
Example:

``````julia> findall(x -> x.k == 2, A)
3-element Vector{Int64}:
7
8
9
``````
1 Like

That was a great solution, thanks @oheil !

1 Like

An alternative, that outputs the required `true` or `false`:

``````any(x -> x.k == 3, A)
``````
3 Likes

Nice and fast, here is a similar one too:

``````any(s.k==3 for s in A)
``````
3 Likes