Thread Tools Display Modes
12-25-06, 10:36 AM   #1
sibbor
A Deviate Faerie Dragon
Join Date: Nov 2005
Posts: 10
Question Arrays

I've had a look at this URL: http://www.wowinterface.com/forums/s...t=table.insert and the LUA documentation but still can't seem to find a solution for my problem:
I can't insert data into a array that looks like this:
Code:
arrayName = {
    {
        nick="nick 1",
        name="name 1",
        age="age 1",
    },
    {
        nick="nick 2",
        name="name 2",
        age="age 2",
    },
};
If I manually set the array to look like above I can fetch the data with this code:
Code:
nick = arrayName[i].nick);
name = arrayName[i].name);
age = arrayName[i].age);
I've tested the following:
Code:
arrayName = {}

table.insert(arrayName.i.nick, nick);
table.insert(arrayName.i.name, name);
table.insert(arrayName.i.age, age);
And this:
Code:
arrayName = {}

table.insert(arrayName[i].nick, nick);
table.insert(arrayName[i].name, name);
table.insert(arrayName[i].age, age);
This:
Code:
arrayName = {}

table.insert(arrayName[i]."nick", nick);
table.insert(arrayName[i]."name", name);
table.insert(arrayName[i]."age", age);
... and this:
Code:
arrayName = {}

table.insert(arrayName[i]["nick"], nick);
table.insert(arrayName[i]["name"], name);
table.insert(arrayName[i]["age"], age);

Last edited by sibbor : 12-25-06 at 10:47 AM.
  Reply With Quote
12-25-06, 10:52 AM   #2
Syzgyn
An Aku'mai Servant
 
Syzgyn's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 37
I believe the problem is you're not adding [i] in. Try this:
Code:
arrayName = {
   [1]= {
        ["nick"]="nick 1",
        ["name"]="name 1",
        ["age"]="age 1",
    },
    [2]={
        ["nick"]="nick 2",
        ["name"]="name 2",
        ["age"]="age 2",
    },
};
It might be a bit excessive, but it works and is easy to read (IMO)
Then you can access it like this:
Code:
nick = arrayName.[i].nick
name = arrayName.[i].name
age = array[i].age
If you're still confused, take a look at http://www.lua.org/pil/3.6.html
  Reply With Quote
12-25-06, 10:58 AM   #3
sibbor
A Deviate Faerie Dragon
Join Date: Nov 2005
Posts: 10
Originally Posted by Syzgyn
I believe the problem is you're not adding [i] in. Try this:
Code:
arrayName = {
   [1]= {
        ["nick"]="nick 1",
        ["name"]="name 1",
        ["age"]="age 1",
    },
    [2]={
        ["nick"]="nick 2",
        ["name"]="name 2",
        ["age"]="age 2",
    },
};
I've thought about this too. I'll have a look at it then. I hope I don't run into the same problem again; that I can't insert the data... Then I'll get back to you :- ).

Then you can access it like this:
Code:
nick = arrayName.[i].nick
name = arrayName.[i].name
age = array[i].age
Aye, I see.

If you're still confused, take a look at http://www.lua.org/pil/3.6.html
Thanks for the link :- )
  Reply With Quote
12-25-06, 02:19 PM   #4
Heron
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 9
Try something like this:
Code:
temp = {
        nick="nick n",
        name="name n",
        age="age n",
    }

table.insert(arrayName, temp);
EDIT: And another thing, in LUA is called table, not array.

Last edited by Heron : 12-25-06 at 02:22 PM.
  Reply With Quote
12-25-06, 02:24 PM   #5
sibbor
A Deviate Faerie Dragon
Join Date: Nov 2005
Posts: 10
Originally Posted by Heron
Try something like this:
Code:
temp = {
        nick="nick n",
        name="name n",
        age="age n",
    }

table.insert(arrayName, temp);
I made it myself, but thanks either way.

Originally Posted by Heron
EDIT: And another thing, in LUA is called table, not array.
Yeah, noticed ;-)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Arrays


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