Alright, I've look around and can't seem to find this out: When do string keys need to be enclosed in quotes?
For example, I'm trying to declare an empty nested tabled (2 levels). It couldn't be easy like most programming languages, could it? Example:
local var = {
key1 = {},
key2 = {}
}
Throws an error. Wrapping the two keys in quotes throw an error as well. The only thing I've found that actually works is via the object method:
local var = {}
var.key1 = {}
var.key2 = {}
Works. But then of course, if I want to add to it or reference it in any way, I have to use quotes, unless I use the object method.
So is it that you can declare a nested table with the keys not being quoted as long as there's actual data being supplied? Example:
local var = {
key1 = {"val1", "val2", "va3"},
key2 = "val4",
}
Throws no errors... What the heck? :rolleyes:
While I'm asking stuff, some languages distinguish between single and double quotes (single = literal string, double = parsed vars, etc), is that the case with LUA or does it matter? My syntax highlighting isn't working for single quotes so I was going to change them all. >.<
Hmm, the first code block should work. What's the error? Basically Lua table keys work like JavaScript, so if you want a space or most punctuation in the key, use quotes. I think double and single quotes are equivalent in Lua. I write in too many languages to truly have the intricacies of Lua memorized though, so any of this could be wrong.
Well, the first block was throwing a "index is nil" error. I mean, I got it working but bleh, I'd like to know what to expect. I can't find any good info in the LUA docs. Though I did find something that mentions that you have to declare the parent table as a table first. Even doing that, I still had to use the object method to declare the nested tables.
Remember that square brackets are mandatory as soon as you do not use "string literals". Example:
tbl = {
k = 5, -- OK, same as ["k"] = 5
[1] = 4, -- OK
[true] = 5, -- OK
40 = 48, -- fail, should be [40] = 48
true = 8, -- fail, true is a reserved keyword, should be ["true"] = 8
}
you know if you use "/print" from ace2 or rock or wherever. It will show you all the places you have to use quotes, since its output can pretty much be used to define the table it is printing (for strings, number, tables)
Not even that, as long as the key isn't a reserved keyword or contains spaces/punctuation. Again think JavaScript if you have experience there.
I thought you had to enclose them in strings when calling them (IE to get a value, etc). What I mean is:
t = {
keya = 1,
keyb = 2
}
var = t[keya] -- throws an error because the engine thinks keya is an undefined var (nil)
var = t["keya"] -- OK
var = t.keya -- OK
That reminds me.. I've seen some tables where the last entry had a comma:
t = {
ta = {},
tb = {},
}
That should be a legitimate declaration?
you know if you use "/print" from ace2 or rock or wherever. It will show you all the places you have to use quotes, since its output can pretty much be used to define the table it is printing (for strings, number, tables)
I didn't know that. So do I just paste the code after typing /print ?
I thought you had to enclose them in strings when calling them (IE to get a value, etc). What I mean is:
t = {
keya = 1,
keyb = 2
}
var = t[keya] -- throws an error because the engine thinks keya is an undefined var (nil)
var = t["keya"] -- OK
var = t.keya -- OK
As soon as you use the square brackets [], you need to supply a string key, either as a string literal or as the value contained in a variable. You can use dot notation to get to a table key without using [""] as long as the key is valid out in the open like that.
Edit: You can also put a number in the brackets [1] to directly index into the table. Tables and strings are 1-based in Lua, unlike many other languages which are 0-based.
All I have to say is, bracketed keys always work. :)
Anyway, I skimmed the thread and didn't see if someone touched on this, but if you use variables to access table keys, they must be in brackets no matter how they were declared.
local var = "key"
local t = {
key = "value",
}
print(t[var]) -- prints "value"
print(t["var"]) -- accessing field var (a nil value)
print(t.var) -- accessing field ? (a nil value)
* I might have the error messages reversed or one of them wrong, but you get the idea.
StormFX: I suggest you go through these basic tutorials on lua from lua-users.org. They helped me understand things at the beginning. http://lua-users.org/wiki/TutorialDirectory
If you want a lua interpreter to go through them with, then you can use WebLua (an online lua interpreter) or Lua-WoW (run on your own machine) from here: http://wowprogramming.com/utils
StormFX: I suggest you go through these basic tutorials on lua from lua-users.org. They helped me understand things at the beginning. http://lua-users.org/wiki/TutorialDirectory
If you want a lua interpreter to go through them with, then you can use WebLua (an online lua interpreter) or Lua-WoW (run on your own machine) from here: http://wowprogramming.com/utils
Thanks for links. I've went through most of the docs, but it doesn't cover everything. >.<
Seriously. Read it carefully, even the parts you assume you can skip because you already "know programming". Good stuff in there. If you can afford the 2nd edition, it's even better. The first edition has the useful property of not costing money though.
Most folks hit lua.org and go right to the reference manual page. It's a great reference, obviously, but it's not that great of a textbook, nor was it meant to be.
What's the best to get a key count on a table since getn(t) and (#t) are screwy?
They're "screwy" only if your table has holes, or if you're using the hash part of a table. They work fine if you're using the simple numeric array part of a table. Because "how many things are in this hash table?" can be answered in different ways depending on the application at hand -- and more to the point, metatables could affect the answer -- they didn't try to guess how you want the question to be answered.
local function KeyCount(t)
local i = 0
for _, v in pairs(t) do
if v ~= nil then
i = i + 1
end
end
return i
end
Is there a better way to do this? >.<
That'd work. No need to test for nil. If it's nil, it can't be in the table.
edit: by which I mean this would do the same thing but shorter:
local function KeyCount(t)
local i = 0
for _ in pairs(t) do
i = i + 1
end
return i
end
For example, I'm trying to declare an empty nested tabled (2 levels). It couldn't be easy like most programming languages, could it? Example:
Throws an error. Wrapping the two keys in quotes throw an error as well. The only thing I've found that actually works is via the object method:
Works. But then of course, if I want to add to it or reference it in any way, I have to use quotes, unless I use the object method.
So is it that you can declare a nested table with the keys not being quoted as long as there's actual data being supplied? Example:
Throws no errors... What the heck? :rolleyes:
While I'm asking stuff, some languages distinguish between single and double quotes (single = literal string, double = parsed vars, etc), is that the case with LUA or does it matter? My syntax highlighting isn't working for single quotes so I was going to change them all. >.<
Gotcha. So I shouldn't have to quote the keys at declaration, only when calling them, right?
Not even that, as long as the key isn't a reserved keyword or contains spaces/punctuation. Again think JavaScript if you have experience there.
I thought you had to enclose them in strings when calling them (IE to get a value, etc). What I mean is:
That reminds me.. I've seen some tables where the last entry had a comma:
That should be a legitimate declaration?
I didn't know that. So do I just paste the code after typing /print ?
As soon as you use the square brackets [], you need to supply a string key, either as a string literal or as the value contained in a variable. You can use dot notation to get to a table key without using [""] as long as the key is valid out in the open like that.
Edit: You can also put a number in the brackets [1] to directly index into the table. Tables and strings are 1-based in Lua, unlike many other languages which are 0-based.
Yes, and it's also legal in C# and others. JavaScript doesn't like it, however, much to my disappointment.
/print {ta = {}, ["tb"] = {}, }
does this:
to reduce any potential Addon issues.
Looting changed to Group Loot.
Loot threshold set to Uncommon.
Prat 3.0 Beta Version (DEBUG)
Prat: Module Count: 33 total 24 loaded, 22 enabled
Prat: Memory Use: 540 KB
Joined Channel: [1. General - Burning Steppes]
Joined Channel: [3. LocalDefense - Burning Steppes]
{ -- table: 1E388FA0
ta = {},
tb = {}
} -- table: 1E388FA0
Hmm.. On my machine, both Rock and Ace return a similar error:
Ace:
Rock:
Anyway, I skimmed the thread and didn't see if someone touched on this, but if you use variables to access table keys, they must be in brackets no matter how they were declared.
* I might have the error messages reversed or one of them wrong, but you get the idea.
http://lua-users.org/wiki/TutorialDirectory
If you want a lua interpreter to go through them with, then you can use WebLua (an online lua interpreter) or Lua-WoW (run on your own machine) from here: http://wowprogramming.com/utils
Thanks for links. I've went through most of the docs, but it doesn't cover everything. >.<
Seriously. Read it carefully, even the parts you assume you can skip because you already "know programming". Good stuff in there. If you can afford the 2nd edition, it's even better. The first edition has the useful property of not costing money though.
Most folks hit lua.org and go right to the reference manual page. It's a great reference, obviously, but it's not that great of a textbook, nor was it meant to be.
What's the best to get a key count on a table since getn(t) and (#t) are screwy? What I've done so far is this:
The table:
The function:
Is there a better way to do this? >.<
They're "screwy" only if your table has holes, or if you're using the hash part of a table. They work fine if you're using the simple numeric array part of a table. Because "how many things are in this hash table?" can be answered in different ways depending on the application at hand -- and more to the point, metatables could affect the answer -- they didn't try to guess how you want the question to be answered.
That'd work. No need to test for nil. If it's nil, it can't be in the table.
edit: by which I mean this would do the same thing but shorter:
Yeah. I just meant that most languages have built-in functions that do what mine does. >.<
Oh, dur... /slapself
Thanks for the help and answers! :) My project is about ready for alpha, but I'm going to play a bit more. :D