Originally Posted by doofus
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?
|
in the example below the loop ranges from 1 to #mydata, which in your example is 4.
Lua Code:
for i=1, #mydata do
if mydata[i] == "somevalue" then
print("bingo")
end
end
Originally Posted by doofus
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.
|
you can e.g. add something if it doesn't exist in mydata
Lua Code:
if not mydata["five"] then
--insert key "five" in mydata
tinsert(mydata, "five")
end
Hope this is somewhat helpful to your situation.