# How to deal with structure elements?

**URL:** <https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814>\
**Category:** General Usage\
**Tags:** question, struct\
**Created:** [November 2, 2021, 3:56pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814 "2021-11-02T15:56:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [November 2, 2021, 3:56pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/1 "2021-11-02T15:56:33Z")

</div>

I have the below MWE, in which at some points I need to check the field `id` of all elements of structure S (i.e., CP) before I continue add elements to CP, but I am having the below error. Any idea to solve it? In other words, why the command `CP[:].id[1]` returns an error, in my mind it should return the first position of field `id` of all elements in CP as matrix do.

```julia
mutable struct S
    id::Vector{String}
end
CP=S[];
push!(CP, S(["3d4f7006", "3d4f7006", "3d4f7006"]));
push!(CP, S(["3d5ce1ef", "3d5ce1ef", "3d5ce1ef"]));
p="3d4f7kkk";
if CP == [] || CP[:].id[1] .!= p
push!(CP, S([p, p, p]));
end
ERROR: type Array has no field id
Stacktrace:
 [1] getproperty(x::Vector{CP}, f::Symbol)
   @ Base .\Base.jl:33
 [2] top-level scope
   @ REPL[8]:1

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 2, 2021, 4:10pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/2 "2021-11-02T16:10:16Z")

</div>

You can do this:

```julia
julia> (x.id[1] for x in CP) .!= p
2-element BitVector:
 1
 1

```

still, note that the comparision will return a vector, such that the conditional there is probably not doing what you want.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 2, 2021, 4:41pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/3 "2021-11-02T16:41:27Z")

</div>

Perhaps you want this?

```julia
julia> if isempty(CP) || all(x -> x.id[1] != p, CP)
       push!(CP, S([p, p, p]));
       end
3-element Vector{S}:
 S(["3d4f7006", "3d4f7006", "3d4f7006"])
 S(["3d5ce1ef", "3d5ce1ef", "3d5ce1ef"])
 S(["3d4f7kkk", "3d4f7kkk", "3d4f7kkk"])

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 2, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/4 "2021-11-02T16:48:58Z")

</div>

Uglier ~~but certainly faster~~ :

```julia
julia> function push_p!(CP,p)
           if isempty(CP)
               push!(CP,S([p,p,p]))
               return CP 
           end
           for s in CP
               s.id[1] == p && return CP
           end
           push!(CP,S([p,p,p]))
           return CP
       end
push_p! (generic function with 1 method)

julia> push_p!(CP,p)
3-element Vector{S}:
 S(["3d4f7006", "3d4f7006", "3d4f7006"])
 S(["3d5ce1ef", "3d5ce1ef", "3d5ce1ef"])
 S(["3d4f7kkk", "3d4f7kkk", "3d4f7kkk"])

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 2, 2021, 4:57pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/5 "2021-11-02T16:57:01Z")

</div>

Interesting that this is faster, I would have expected these to be equivalent?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 2, 2021, 5:10pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/6 "2021-11-02T17:10:36Z")

</div>

Yeah, you’re right, at first sight I thought that `all` would apply the condition to all elements, but of course that is not the case. Probably using it is a good idea because of more careful handling of inbounds and stuff.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [November 2, 2021, 5:47pm UTC](https://discourse.julialang.org/t/how-to-deal-with-structure-elements/70814/7 "2021-11-02T17:47:41Z")

</div>

@jishnub @lmiq Thank you very much!
