Pulling my hair with LUA
If I have this structure
mydata = { "one", "two", "three", "four" }; How do I iterate through it? How do I add a value if I want to? I am thinking of iterating like mydate[1], mydata[2] etc but the for loop syntax confuses me. In C it might look like this for i = 1, 50 do if ( mydata[i] == somevalue ) then return/break/whatever; -- we already have it end if ( i == 51 ) then ... -- we do not have it, add it mydata[51] = somevalue; end I am not intending to remove just to add and then at some time wipe the lot and start again. |
Forgot to say, sure I can do mydata["one"] = 1;
and then mydata[somevalue] will be nil if it does not exist, but using a map for a simple array? |
Quote:
Lua Code:
Quote:
Lua Code:
Hope this is somewhat helpful to your situation. |
Lua Code:
To explain what pairs and ipairs do, they essentially just return an index key and value associated with that key in pairs. Lua Code:
When to use pairs/ipairs?
|
Thank you! I now know how to use ipairs and especially #mydata syntax I had no idea...
|
For more reference...
http://lua-users.org/wiki/TutorialDirectory |
All times are GMT -6. The time now is 05:26 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI