Iterating Through an Array - Wrong Order?

Hey everyone,

I’ve a weird problem. I have a main array that holds items, and within that array I have an additional array for each item (so an array in an array).

When I do a regular loop to go through the items in the main array, it doesn’t loop in order. It does a random order instead. I want to loop through the secondary arrays in the order they were inserted into the main array.

Example:

  
mainArray = {  
  
item1 = {name = "Hammer" , cost = 100, imgName = "Hammer.png"},  
item2 = {name = "Sword" , cost = 500, imgName = "Sword.png"},  
item3 = {name = "Gun" , cost = 1000, imgName = "Gun.png"},  
  
}  
  

This is not my actual code, but it show what I’m doing. I have around 12 items inserted into the main array, and then when looping through the main array and retrieving each secondary array, they don’t come in the correct order.

Any suggestions?

PS. I’m looping through using some code like this:

[code]

for i,value in pairs(mainArray) do

end

[/code] [import]uid: 51654 topic_id: 16395 reply_id: 316395[/import]

The way you have used tables in the above example is like a dictionary datatype and elements won’t be stored in any particular order


There is no guarantee as to the order in which keys will be stored in a table when using dictionaries so the order of retrieval of keys using pairs() is not guaranteed.

-Lua Wiki

I think you should use
[lua] table.insert(mainArray,secondaryTable,1) [/lua]
or
[lua] mainArray[1]= secondaryTable[/lua] [import]uid: 64174 topic_id: 16395 reply_id: 61171[/import]

just change the declaration slightly as

mainArray = {   
 {name = "Hammer" , cost = 100, imgName = "Hammer.png"},  
 {name = "Sword" , cost = 500, imgName = "Sword.png"},  
 {name = "Gun" , cost = 1000, imgName = "Gun.png"},  
}  

and now if you use the

for i,value in pairs(mainArray) do  
 item = mainArray[i]  
 print(item.name)  
 print(item.cost)  
 print(item.imgName)  
end  

you will get what you originally intended

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16395 reply_id: 61214[/import]

@JayantV

Wow, thanks for that! I would never have realized that. The reason I put item 1, item 2 etc. is so that I can easily do mainArray[nameOfItem].cost or something like that. Otherwise, that method would work perfectly.

Thanks again for the help! :slight_smile: [import]uid: 51654 topic_id: 16395 reply_id: 61233[/import]

You can still do that if you loop though the items like this :

[code]

mainArray = {

[1] = {name = “Hammer” , cost = 100, imgName = “Hammer.png”},
[2] = {name = “Sword” , cost = 500, imgName = “Sword.png”},
[3] = {name = “Gun” , cost = 1000, imgName = “Gun.png”},

}

for i = 1, #mainArray do
print(nameArray[i].name)
end
[/code] [import]uid: 84637 topic_id: 16395 reply_id: 61237[/import]

@Danny, don’t you think that the

{  
[1] = {},  
[2] = {},  
[3] = {}   
}  

is kind of redundant and unnecessary where it is implicitly added when you just use

{  
 {},  
 {},  
}  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16395 reply_id: 61242[/import]

@Danny But you still have to access a spot in the array by using mainArray[i].name. If I had about 50 different items, and I wanted to get the stat for particular item, I wouldn’t know which index it’s stored in. With my code in the first post, I could just do mainArray[rocketLauncher].cost and it would work.

@JayantV Yeah I guess it is redundant, but it makes it easy to take a look at the array and easily tell which index something is stored in (if you have a lot of items). [import]uid: 51654 topic_id: 16395 reply_id: 61243[/import]

@Naveen,
you can use *constants*, something like the c enums, which can help you go to the item you need, that way you can use it the way you originally intended to.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16395 reply_id: 61246[/import]