# Can this be done using a struct?

**URL:** <https://discourse.julialang.org/t/can-this-be-done-using-a-struct/26833>\
**Category:** General Usage\
**Created:** [July 26, 2019, 7:57am UTC](https://discourse.julialang.org/t/can-this-be-done-using-a-struct/26833 "2019-07-26T07:57:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 26, 2019, 7:57am UTC](https://discourse.julialang.org/t/can-this-be-done-using-a-struct/26833/1 "2019-07-26T07:57:19Z")

</div>

Hey guys.

Say that I am reading an XML file and getting the following information:

```julia
k[1:2]
2-element Array{XMLElement,1}:
 <floating mkbound="1" mk="12" begin="7206" count="21" property="hard-wood">
                <massbody value="2.52" units_comment="kg"/>
                <masspart value="0.12" units_comment="kg"/>
                <center x="0.2" y="0" z="0.2" units_comment="metres (m)"/>
                <inertia units_comment="kg*m^2">
                    <values v11="0.001632" v12="0" v13="2.60209e-20"/>
                    <values v21="0" v22="0.003264" v23="0"/>
                    <values v31="2.60209e-20" v32="0" v33="0.001632"/>
                </inertia>
            </floating>
 <floating mkbound="2" mk="13" begin="7227" count="21" property="hard-wood">
                <massbody value="2.52" units_comment="kg"/>
                <masspart value="0.12" units_comment="kg"/>
                <center x="0.36" y="0" z="0.2" units_comment="metres (m)"/>
                <inertia units_comment="kg*m^2">
                    <values v11="0.001632" v12="0" v13="2.60209e-20"/>
                    <values v21="0" v22="0.003264" v23="0"/>
                    <values v31="2.60209e-20" v32="0" v33="0.001632"/>
                </inertia>
            </floating>

```

Basically I want to save the data, so I prefer to not use a function but a struct, and I want to make it so that the data is accessed by applying the “mk” property. So for an example I am making a struct like this:

```julia
 struct Floating
       mk
       mkbound
       beg
       cou
       property
 end

```

(It is not letting me use “begin” since it is a definition and “count” is a function, don’t know if I can over rule it or if it even makes sense to do so)

Now assigning some random values:

```julia
x = Floating(12,1,0,100,"wood")
Floating(12, 1, 0, 100, "wood")

```

I can now access these fields by simple dot notation, ie. x.mkbound, but I would like to make it in a way where I write:

```julia
x(mkbound=12)[beg,cou]

```

Which means, for the floating with the property mkbound=12, return “begin” and “count” ie. 0 and 100 for this example.

I want to do it this way using a struct, and not a function, since I am using LightXML to read the XML file and would like to free up the memory and save the data in an active Julia editor, for an interactive application.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 26, 2019, 8:21am UTC](https://discourse.julialang.org/t/can-this-be-done-using-a-struct/26833/2 "2019-07-26T08:21:15Z")

</div>

I’ve written a function so now I am able to get all values into a struct array:

```julia
function Access(k::Array{XMLElement,1})
    n = length(k)
    j = Array{Floating,1}(undef,n)
    for i = 1:n
        mkbound = attribute(k[i],"mkbound")
        mk = attribute(k[i],"mk")
        beg = attribute(k[i],"begin")
        count = attribute(k[i],"count")
        property = attribute(k[i],"property")
        j[i] = Floating(mkbound,mk,beg,count,property)
    end
    return j
end

```

Now I just need to figure out how to search the array based on property.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [July 26, 2019, 10:27am UTC](https://discourse.julialang.org/t/can-this-be-done-using-a-struct/26833/3 "2019-07-26T10:27:50Z")

</div>

You can use `findfirst()` or `findall()` functions for search to get the indices satisfying a predicate:

```julia
# finds the indices of the elements whose :beg is > 1
findall(x->x.beg>1, arr)

```

A simple wrapper may be more convenient for search of a specific value for a field:

```julia
"""
Return the indices of all elements `elt` 
such that `predicate(elt.fieldname)` returns `true`.
"""
function findfield(predicate::Function, A, fieldname)
    fieldname = Symbol(fieldname)
    findall(x->predicate(getfield(x, fieldname)), A)
end

```
