Table in Table with custom key name - lua

I have to declare a table in a table that will act like this:
table = {'79402d' = {'-5.4','5','1.6'}, '5813g1' = {'3','0.15','18'}}
So when I loop through it, I can use something similar to table['79402d'][0] to print coordinates.

There are two types of syntax for table constructors. The general form:
t = { ['key'] = value }
(If the key is a valid identifier)The syntax sugar form:
t = { key = value }
Here you are mixing them up. Because 79402d isn't a valid identifier (beginning with letters or underscore), you have to use the general form:
t = {['79402d'] = {'-5.4','5','1.6'}, ['5813g1'] = {'3','0.15','18'}}

Related

Obtain values from embedded tables Lua

I am new to Lua and I am trying to learn how to make a function with embedded tables. I am stuck trying to figure out a way to make the function meet specific values in the table.
Here is an example of a table:
TestTable = {destGUID1 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}},destGUID2 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}}}
Now I want to make a function for this table that pulls values only from the specific destGUID. Like:
function CatInfo(GUID,Cat)
for i=1, #TestTable do
if TestTable[i] == GUID then
for j=1, TestTable[i][GUID] do
if TestTable[i][GUID][j] == Cat then
return TestTable[i][GUID][Cat].A -- returns value "A"
end
end
end
end
end
So that when I use this function, I can do something like this:
CatInfo(destGUID2,catagory1) -- returns "1"
Given your table structure, you don't need to do any looping; you can simply return the value from the table based on GUID and the category:
TestTable = {
destGUID1 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}},
destGUID2 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}}
}
function CatInfo(GUID,Cat)
return TestTable[GUID][Cat].A
end
print(CatInfo('destGUID2','catagory1'))
This will print 1. Note that destGUID2 and catagory1 need to be in quotes as those are strings.

lua: user input to reference table

I am having trouble with my tables, I am making a text adventure in lua
local locxy = {}
locxy[1] = {}
locxy[1][1] = {}
locxy[1][1]["locdesc"] = "dungeon cell"
locxy[1][1]["items"] = {"nothing"}
locxy[1][1]["monsters"] = {monster1}
The [1][1] refers to x,y coordinates and using a move command I can successfully move into different rooms and receive the description of said room.
Items and monsters are nested tables since multiple items can be held there (each with their own properties).
The problem I am having is getting the items/monsters part to work. I have a separate table such as:
local monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "a rat"
monsters["rat"]["Health"] = 5
monsters["rat"]["Attack"] = 1
I am using a table like this to create outlines for various enemy types. The monster1 is a variable I can insert into the location table to call upon one of these outlines, however I don't know how to reference it.
print("You are in ", locxy[x][y]["locdesc"]) -- this works
print("You can see a ", locxy[x][y]["monsters]["Name"],".") - does not work
So I would like to know how I can get that to work, I may need a different approach which is fine since I am learning. But I would also specifically like to know how to / if it possible to use a variable within a table entry that points to data in a separate table.
Thanks for any help that can be offered!
This line
locxy[x][y]["monsters]["Name"]
says
look in the locxy table for the x field
then look in the y field of that value
look in the "monsters"` field of that value
then look in the "Name" field of that value
The problem is that the table you get back from locxy[x][y]["monsters"] doesn't have a "Name" field. It has some number of entries in numerical indices.
locxy[x][y]["monsters][1]["Name"] will get you the name of the first monster in that table but you will need to loop over the monsters table to get all of them.
Style notes:
Instead of:
tab = {}
tab[1] = {}
tab[1][1] = {}
you can just use:
tab = {
[1] = {
{}
}
}
and instead of:
monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "foo"
you can just use:
monsters = {
rat = {
Name = "foo"
}
}
Or ["rat"] and ["Name"] if you want to be explicit in your keys.
Similarly instead of monsters["rat"]["Name"] you can use monsters.rat.Name.

Lua Tables Assigning values to key in tables

Um what's the difference between in assigning values:
Lesson["Maths"] = {grade = 10, class = 3}
and
Lesson["Maths"] = {["grade"] = 10, ["class"] = 3}
Which one should I go if I want to assign a value to a key in a table? Or which one is more used? Thanks a lot
In your case, both of them are the same. The first usage fails when you want keys with special characters in them:
tEx = {
failed-approach = true,
}
The code segment above will result in an error and you would be forced to follow the second method of creating keys:
tEx = {
['failed-approach'] = false,
}
Both of the approaches are same and it doesn't matter which one you wish to use.

lua - Access table item from within the table

I'm trying to create an object oriented implementation in Lua, for example:
Parent = {
ChildVariable = "Hello",
ChildFunction = function ()
print(Parent.ChildVariable)
end
}
What I would like is rather than doing Parent.ChildVariable I can do ChildVariable instead; it is in the table so I thought must be some way to access it.
Parent = {
ChildVariable = "Hello",
ChildFunction = function(self)
print(self.ChildVariable)
end
}
Parent:ChildFunction()
Lua has a special construct for that: the colon operator.
The two following lines are equivalent:
tbl.func(tbl)
and
tbl:func()

Lua key name starts with digit in table statement

When key name starts with digit, in javascript we can define array-like object like this:
var table = {
'123.com': 'details'
'456.net': 'info'
}
But when I try these code in Lua5.1:
table = { '123.com' = 'info' }
It throws an error:
[string "local"]:1: '}' expected near '='
But these code are accepted in lua:
table = {}
table['123.com'] = 'info'
I wonder if it is a bug in Lua5.1. Or did I missed something?
When creating a Lua table using the literal table constructor, non-identifier table indices should be enclosed in square brackets. For example:
table = { ['123.com'] = 'info' }
(From: http://www.lua.org/pil/3.6.html)

Resources