Thread Tools Display Modes
08-26-16, 04:35 PM   #1
Twitchy250
A Deviate Faerie Dragon
Join Date: Aug 2016
Posts: 14
Lua loop Multidementional Array help

Hello wowinterface,

I am beginning learning Lua to start writing wow addons. ( I do have prior experience due to html and php)

I am trying to loop through a multidementional array displaying certain info from them.

Lua Code:
  1. function HelloWorld()
  2.   a = {{1,2,3}, {4,5,6}, {7,8,9}}
  3.   m = print (#a);
  4.  
  5.   for i=0,3,1
  6.   do  
  7.     print(a[1]);
  8.  
  9.   end
  10. end

This example works except the outputting of the array which seems to display random data.

The second part of my problem is the array would be a variable legnth so using 3 as a peramiter is too specific to properly pull out data.

Lua Code:
  1. function HelloWorld()
  2.   a = {{1,2,3}, {4,5,6}, {7,8,9}}
  3.   m = print (#a);
  4.  
  5.   for i=0,m,1
  6.   do  
  7.     print(a[i][1]);
  8.  
  9.   end
  10. end

I've tried several different things debugging, including the example above grabbing the number of sections in the array to determine how many times to loop, but using a variable instead of an exact number seems to break the for loop and doesn't output anything.

I've also done several google searches.

It would be much appreciated if someone could help me fix the issue or let me know of a better way of doing this.

All help is appreciated! Thanks
  Reply With Quote
08-26-16, 06:12 PM   #2
syncrow
A Flamescale Wyrmkin
 
syncrow's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 149
Lua Code:
  1. function PrintArray(array)
  2.     for k, v in pairs(array) do
  3.         if type(v) == "table" then
  4.             print(v, "contains:")
  5.             PrintArray(v)          
  6.         else
  7.             print(v)
  8.         end
  9.     end
  10. end

just call this function like this:
Lua Code:
  1. local a = {
  2.     {1,2,3},
  3.     {4,5,6},
  4.     {7,8,9},
  5.     [8] = "HELP",
  6.     [12] = {
  7.         ["MEW"] = true,
  8.         "foul",
  9.     },
  10. }
  11.  
  12. PrintArray(a)
__________________

Last edited by syncrow : 08-26-16 at 06:19 PM.
  Reply With Quote
08-26-16, 06:24 PM   #3
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Hi!

Lua Code:
  1. function HelloWorld()
  2.   a = {{1,2,3}, {4,5,6}, {7,8,9}}
  3.   m = print (#a);
  4.  
  5.   for i=0,3,1
  6.   do  
  7.     print(a[1]);
  8.  
  9.   end
  10. end

Didn't understand if you have any issues with this example, but the loop doesn't really do anything here, since you don't use the incrementor and instead just print the same first element (Lua tables start on 1) of table a for all four iterations of the loop. That element happens to be another table, and when you print that using the function with the same name, you will just get the table's memory address or some such thing, which isn't really useful for anything.

Using variables as the upper for loop limit is fine. The problem here should that you use 0 as the lower limit, and since Lua tables start at 1 (and you haven't explicitly added an element at index 0) the very first iteration will error and halt the execution. I'm guessing you don't have Lua errors enabled, which would've helped spotting this error!

Code:
/console scriptErrors 1
Once you get the loop working, it's just a matter of adding another loop inside the first one to iterate over the deepest tables.

You may also use ipairs (for "arrays") or pairs (for when arbitrary keys are used) to have some of the work done for you.

Code:
a = {{1,2,3}, {4,5,6}, {7,8,9}}
for i = 1, #a do
	local v = a[i]
	print(v)
end
Code:
a = {{1,2,3}, {4,5,6}, {7,8,9}}
for i, v in ipairs(a) do
	print(v)
end
As far as arrays go, these two examples are functionally identical.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
08-26-16, 08:44 PM   #4
Twitchy250
A Deviate Faerie Dragon
Join Date: Aug 2016
Posts: 14
Smile

Thanks all for the quick reply.

I think I may have worded my issue poorly but this helped me with my problem.

Lua Code:
  1. a = {{1,2,3}, {4,5,6}, {7,8,9}}
  2.   for i = 1, #a do
  3.     local v = a[i]
  4.     print(v)
  5.   end

All I had to do was add a [1] to the end of the local v line.

If I hadn't been clear to clarify
For an array to have name=this, color=this, name=that, color=that
I could grab each name without grabbing the color.

The examples in the first post were a little beyond me this early in learning lua, But thank you for all for the help!

It is much appreciated
  Reply With Quote
08-26-16, 08:59 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
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 » Lua loop Multidementional Array help


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