Thread Tools Display Modes
02-24-09, 06:19 PM   #1
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Expanding tables with loops

I have a simple question ... my current test code (tested in Wowlua and Omnibus, both times the same error) seems to have a bug - but i am unable to get it ...


Error Message: attempt to index field '?' ( a nil value )

Testcode:
Code:
local x = {}
local k = 1

while name~=nil do
   local name,rank,rankIndex,level,LOclass,zone,_,_,online,status,englishClass =GetGuildRosterInfo(k)
   x[k].n="Test"
   x[k].r=rank
   x[k].l=level
   x[k].c=LOclass
   x[k].z=zone
   x[k].on=online
   x[k].realClass=englishClass
   k=k+1
end

DEFAULT_CHAT_FRAME:AddMessage(x[1].n)
hope someone can enlight me
  Reply With Quote
02-24-09, 06:51 PM   #2
DreamStorm
A Deviate Faerie Dragon
 
DreamStorm's Avatar
Join Date: Feb 2009
Posts: 15
I am kind of new to this lua game myself, so apologies if I am a little off the mark ....

I am assuming that you wish to create the data in the table from the returns from the GetGuildRosterInfo() method. Your while loop is set to test the name part of the return but you haven't got a value for that before you enter the loop; at least not the value you wish. As it is the while loop is essentially evaluating a global variable for nil value. I don't know if there is a global variable called name but if there isn't you're loop will never start as if will only do so if the value is not (~=) nil.

I think you would need to run that method outside the loop, then enter it, set the values and within the loop (at the end) rerun the method. I am not sure how lua handles reusing the varables in this way though. Something like ...

Code:
local x = {}
local k = 1

local name,rank,rankIndex,level,LOclass,zone,_,_,online,status,englishClass =GetGuildRosterInfo(k)
while name~=nil do
   x[k].n="Test"
   x[k].r=rank
   x[k].l=level
   x[k].c=LOclass
   x[k].z=zone
   x[k].on=online
   x[k].realClass=englishClass
   k=k+1   
   name,rank,rankIndex,level,LOclass,zone,_,_,online,status,englishClass =GetGuildRosterInfo(k)
end

DEFAULT_CHAT_FRAME:AddMessage(x[1].n)
You may have to give the returns from the method more specific global names rather than declare them local. Please someone correct me if I am wrong.
__________________


ultima ratio regum
  Reply With Quote
02-24-09, 07:21 PM   #3
Tuhljin
A Flamescale Wyrmkin
 
Tuhljin's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 106
DreamStorm is right about name using the global variable name instead of the local one you want, but the actual error message is due to your trying to index a non-existent table. Try something like this:

Code:
local x = {}
local k = 1

-- Get name before the while loop:
local name,rank,rankIndex,level,LOclass,zone,_,_,online,status,englishClass =GetGuildRosterInfo(k)

while name~=nil do
   x[k] = {}
   x[k].n="Test"
   x[k].r=rank
   x[k].l=level
   x[k].c=LOclass
   x[k].z=zone
   x[k].on=online
   x[k].realClass=englishClass
   k=k+1
   name,rank,rankIndex,level,LOclass,zone,_,_,online,status,englishClass =GetGuildRosterInfo(k)
end

DEFAULT_CHAT_FRAME:AddMessage(x[1].n)
  Reply With Quote
02-25-09, 03:55 AM   #4
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
yea thanks :P - after i went to bed yesterday ... it was quite clear to me ... maybe it was just to late ...

x[k] = {}
-> creates the indicies ?

like
x = {
[1] = { }
}

?

Last edited by Hati-EK : 02-25-09 at 03:58 AM.
  Reply With Quote
02-25-09, 04:44 AM   #5
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
yup in short you are using 2 tables without saying the second time the second is a table
__________________
If you need to reach me I'm in ESO, @class101 or "Fathis Ules i"
addons: SpamBayes, BrokerCPU
projects: ThunderBayes
Mera[xeh]? - La CroisadeEcarlate (wow)
  Reply With Quote
02-26-09, 10:11 AM   #6
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Question:
if i use 'break' like
Code:
for i=1,10 do
 while j<=x do
  if bla==1 then
   value1 = i
   value2 = j
   break
  end
 end
end
will the 'break'-command escape the for-loop too?
  Reply With Quote
03-01-09, 08:57 PM   #7
tsadok
An Aku'mai Servant
 
tsadok's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 32
Originally Posted by Hati-EK View Post
yea thanks :P - after i went to bed yesterday ... it was quite clear to me ... maybe it was just to late ...

x[k] = {}
-> creates the indicies ?

like
x = {
[1] = { }
}

?
Nearly... x[k] = {} requires x to be declared as a table, and x = { [1] = { } } declares both x and x[1] (using two sets of {} )
  Reply With Quote
03-02-09, 05:50 AM   #8
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
note that I did further test and creating multiple tables with loops in one table takes way much memory than creating multiple stable tables and just changing key and values without calling at other tables inside, I think it allocates way much memory and create a quite big garbarge for nothing helpful
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Expanding tables with loops


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