Indexing Vectors of Structs

Hi all,

for ii in myStructArray
        
        ii.parameterOne = lowercase(ii.parameterOne)
        
        ii.parameterTwo = lowercase(ii.parameterTwo)

        print("On loop iteration " + ii)

    end

I’m looping through an array of my own custom struct called my MyStruct with the array being called myStructArray. I want to access the numerical value of my FOR loop counter ii based upon it’s position within the array at the FOR loop iteration. Is ii the MyStruct object/struct itself, or is it a numerical value that I can use to reference elements within myStructArray? If it’s the former, can I use ii as if it were a numerical value?

Hope all this make sense. If you need any clarification feel free to ask me.

Thanks.

[if you do this
quote=“Name1, post:1, topic:87346”]
for ii in myStructArray
[/quote]
then each ii is not an index, but an element of your vector.

If you want the index, you can write

for ii in eachindex(myStructArray) 

which is the preferred way, or alternatively, but less general

for ii in 1:length(myStructArray) 

I suggest the first alternative.

If you want both the index and the value, you can do

for (i, x) in pairs(myStructArray)
1 Like