Thread Tools Display Modes
03-23-18, 10:14 AM   #1
doofus
A Chromatic Dragonspawn
Join Date: Feb 2018
Posts: 158
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.
  Reply With Quote
03-23-18, 10:22 AM   #2
doofus
A Chromatic Dragonspawn
Join Date: Feb 2018
Posts: 158
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?
  Reply With Quote
03-23-18, 10:27 AM   #3
Lolzen
An Aku'mai Servant
 
Lolzen's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 36
Originally Posted by doofus View Post
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

Originally Posted by doofus View Post
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.
  Reply With Quote
03-23-18, 10:36 AM   #4
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
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.
__________________

Last edited by MunkDev : 03-23-18 at 10:55 AM.
  Reply With Quote
03-23-18, 11:49 AM   #5
doofus
A Chromatic Dragonspawn
Join Date: Feb 2018
Posts: 158
Thank you! I now know how to use ipairs and especially #mydata syntax I had no idea...
  Reply With Quote
03-23-18, 06:52 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
For more reference...
http://lua-users.org/wiki/TutorialDirectory
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Pulling my hair with LUA

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off