• Iterate through a non-numerically indexed table in a specific order?
    4 replies, posted
Say I have a table like [CODE] local t = { ["my thing"] = 100, ["his doodad"] = 2000, ["tim's bicycles"] = 5, } [/CODE] when I used a for loop with 'pairs', it will sometimes be out of order. However whenever I use the function 'PrintTable', it seems to always print the table to my console in the correct order. Is it possible to create an iterative loop that will always loop in the order of 'my thing', 'his doodad', and then 'tims bicycles'? ofcourse I could have each index be numerical and then contain a subtable of just that one value, but if it's possible to do it this way that'd be nice.
Not really. I mean, there are ways for this specific case (the length of string index), but I doubt that's what you're going for.
Yeah string length was a coincidence. I guess the best way to achieve what I want is something like this: [CODE] local t = { [1] = { "my thing", 100 }, [2] = { "his doodad", 2000 }, [3] = { "tims bicycles", 5 }, } for _,v in ipairs( t ) do print( v.1, v.2 ); end; [/CODE] edit: Oh well, thanks for the reply :)
You can just do this as a shortcut: [lua]local t = { { "my thing", 100 }, { "his doodad", 2000 }, { "tims bicycles", 5 }, }[/lua] Also, you can't access numerical fields via the dot operator, and thus this: [lua]print( v.1, v.2 )[/lua] is invalid code.
[QUOTE=EvacX;44412696]You can just do this as a shortcut: [lua]local t = { { "my thing", 100 }, { "his doodad", 2000 }, { "tims bicycles", 5 }, }[/lua] Also, you can't access numerical fields via the dot operator, and thus this: [lua]print( v.1, v.2 )[/lua] is invalid code.[/QUOTE] haha yeah I figured those both out as soon as I started actually writing my code, but thank you very much for the help :)
Sorry, you need to Log In to post a reply to this thread.