WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Pulling my hair with LUA (https://www.wowinterface.com/forums/showthread.php?t=56119)

doofus 03-23-18 10:14 AM

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.

doofus 03-23-18 10:22 AM

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?

Lolzen 03-23-18 10:27 AM

Quote:

Originally Posted by doofus (Post 327330)
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:
  1. for i=1, #mydata do
  2.     if mydata[i] == "somevalue" then
  3.         print("bingo")
  4.     end
  5. end

Quote:

Originally Posted by doofus (Post 327330)
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:
  1. if not mydata["five"] then
  2.     --insert key "five" in mydata
  3.     tinsert(mydata, "five")
  4. end

Hope this is somewhat helpful to your situation.

MunkDev 03-23-18 10:36 AM

Lua Code:
  1. for i=1, #mydata do
  2.     local value = mydata[i]
  3.     -- do something with value in sequential array
  4. end
  5.  
  6. for i, value in ipairs(mydata) do
  7.     -- do something with value in sequential array
  8. end
  9.  
  10. for i, value in pairs(mydata) do
  11.     -- do something with value in associative array
  12. end
  13.  
  14. mydata[#mydata + 1] = newValue -- append sequential
  15. mydata["index"] = newValue -- insert associative
  16. mydata[5] = newValue -- insert associative
  17.  
  18. table.insert(mydata, 1, newValue) -- prepend (put newValue at mydata[1])
  19. table.insert(mydata, newValue) -- append (put newValue at mydata[#mydata + 1])

To explain what pairs and ipairs do, they essentially just return an index key and value associated with that key in pairs.
Lua Code:
  1. -- pairs, literally just a wrapper of std next
  2. function pairs (t)
  3.     return next, t, nil
  4. end
  5.  
  6. -- ipairs, does an incremental lookup and stops when the value is nil
  7. local function iter (a, i)
  8.     i = i + 1
  9.     local v = a[i]
  10.     if v then
  11.         return i, v
  12.     end
  13. end
  14.  
  15. function ipairs (a)
  16.     return iter, a, 0
  17. end

When to use pairs/ipairs?
  • pairs - associative arrays, keys are non-numerical or not in sequence starting at 1.
  • ipairs - numerically indexed arrays where there are no gaps between [1] and however many elements the table has.

doofus 03-23-18 11:49 AM

Thank you! I now know how to use ipairs and especially #mydata syntax I had no idea...

Seerah 03-23-18 06:52 PM

For more reference...
http://lua-users.org/wiki/TutorialDirectory


All times are GMT -6. The time now is 12:51 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI