PDA

View Full Version : questions about variables in LUA.


Ayaka
28-04-2008, 04:55 PM
Search ain't my best friend tho it says 0 results on this question.

But how can I use a variable in a variable?
because i see in some scripts like

variable.[someothervariable]

somehow it doesn't want to work with me

if not PartyscanDBPC.DB.[scantypexxz] then

results in a massive exploding of error messages:

Date: 2008-04-28 16:41:06
ID: 51
Error occured in: Global
Count: 1
Message: ..\AddOns\Partyscan\Partyscan.lua line 193:
'<name>' expected near '['
Debug:
[C]: ?
AddOns:
Gatherer, v3.1.1
Ace2
Partyscan, v1.0
Swatter, v5.0.PRE.2966


because of this the script wont work at all.
Seems like it won't work

I need the variable from scantypexxz at PartyscanDBPC.DB.
so it would turn to PartyscanDBPC.DB.somevalue

It is for me mainly to shorten some procedures like


If ( scantypexxz == "Players" ) then
-- code regarding value players
elseif ( scantypexxz == "Members" ) then
-- code regarding value members
end


and lateron i wanted to use like PartyscanDBPC.DB.[scantypexxz].[uniqueID]
anyone knows whats going wrong?

Because if i look to the other scripts, it should work.
but he's now complaining about the '['

efindel
29-04-2008, 04:02 AM
The period separator and [] are two different ways of indexing a table variable in Lua -- you don't use them together like that. First, you'd need to have a table variable. For example:

DB = {}

DB.a = 1

DB.b = 2

You now have a table named DB. It has two entries, "a", and "b", each of which has a value assigned to it. You can refer to those entries in multiple ways:

DB.a

DB["a"]

which = "a"; DB[which]

That's why it's complaining about the code you have:

DB.[which]

is not legal syntax.

Moongaze
29-04-2008, 07:35 AM
You can also do:

local scantypexxz = PartyscanDBPC.DB.scantypexxz;
If ( scantypexxz == "Players" ) then
-- code regarding value players
elseif ( scantypexxz == "Members" ) then
-- code regarding value members
end


and also

local scantypexxz = PartyscanDBPC.DB.scantypexxz;
If ( scantypexxz.uniqueID == 1 ) then
-- code regarding value 1
elseif ( scantypexxz.uniqueID == 2 ) then
-- code regarding value 2
end

or even

local uniqueID = PartyscanDBPC.DB.scantypexxz.uniqueID;
If ( uniqueID == 1 ) then
-- code regarding value 1
elseif ( uniqueID == 2 ) then
-- code regarding value 2
end

Be sure, though, that when you are trying to access parts of a table ... that the value is a table. So:


myTable = {}; -- creates a table
myTable.a = 1; -- populates data
myTable.b = 2; -- populates data
myTable.subTable = {}; -- creates anothe table INSIDE this table
myTable.invalidTable = 3; -- populates data
x = myTable; -- This is okay!
x = myTable.a -- This is okay!
x = myTable.subTable; -- This is okay!
x = myTable.subTable.a; -- This is okay! Although it will be nil
x = myTable.invalidTable; -- This is okay!
x = myTable.invalidTable.a; -- ERROR! "invalidTable" is not a table, therefore it doesn't have table entries, so "a" is invalid.

Ayaka
29-04-2008, 01:26 PM
The period separator and [] are two different ways of indexing a table variable in Lua -- you don't use them together like that. First, you'd need to have a table variable. For example:

DB = {}

DB.a = 1

DB.b = 2

You now have a table named DB. It has two entries, "a", and "b", each of which has a value assigned to it. You can refer to those entries in multiple ways:

DB.a

DB["a"]

which = "a"; DB[which]

That's why it's complaining about the code you have:

DB.[which]

is not legal syntax.

Thank you. it is working now, toobad it need to have a "prefix" before it works.
But it will be alright, it will still be the same :)
thanks again

Ayaka
29-04-2008, 03:39 PM
I can't edit my postmsg but ok.
I've readed wrong but now i got it (i was just awake when i readed) :P
if i do
some[value]
it referes to
some.("returns the value of value")
now i got it
Thanks!