Can this be done using a struct?

Hey guys.

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

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:

 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:

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:

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

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

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.

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

# 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:

"""
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