Lua Tables Assigning values to key in tables - lua

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.

Related

How exactly do I do this

In lua --
Im making a game where you need to sell items.
I have two tables. One for items,
and one for item costs.
itemcosts = {
"Chewed Gum" == 5,
"Leaf" == 2,
"Lint" == 5,
"Moldy bread crumbs" == 10
}
items = {"Leaf"}
How would I like specify the price? Like I tried
price = itemcosts[items[1]]
but It didn't work.
Any help?
Initialize your itemcosts table like this:
itemcosts = {
["Chewed Gum"] = 5,
Leaf = 2,
Lint = 5,
["Moldy bread crumbs"] = 10
}
When you want to use a table key that's a valid name in Lua, you don't need quotes or anything around it; just use it directly. When it's not a valid name, such as if it has spaces in it in your case, or even if it weren't a string at all, then you need square brackets around it. Also, remember that == is for comparison and = is for assignment.

How to get access to a value inside a table inside array?

I have this structure and I'm trying to access the last index:
table = { {[11] = 22}, {[255] = 1}, {[55] = 1000} }
I have tried this, but it returns me nil
print(table[#table][1])
how do I get that 1000 value?
since you use numeric keys it's actually table[#table][55].

Table in Table with custom key name

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'}}

ComputerCraft cant access data in tables

I want to try and compare the two variables capacity and amount but I have no clue as to how to access the data. I will include screenshots from in-game.
Here is the code:
t=peripheral.wrap("left")
local infoTable = t.getTankInfo("west")
print(infoTable.capacity)
The function returns the following.
{
{
capacity = 16000,
contents = {
id = 0;
amount = 0,
},
},
}
EDIT: I got it. Its a table of tables. so to access it.
infoTable[1].capacity
and for the cotents table
infoTable[1].contents.amount
http://puu.sh/gtzX9/acc0839b11.jpg
http://puu.sh/gtzZW/6b2aa52f12.jpg
foo[bar] takes whatever is in the variable bar and uses it to index foo. Since in your example, variable capacity doesn't exist, it's value is nil.
You want infoTable.capacity, which is the same as infoTable["capacity"]

How to quickly initialise an associative table in Lua?

In Lua, you can create a table the following way :
local t = { 1, 2, 3, 4, 5 }
However, I want to create an associative table, I have to do it the following way :
local t = {}
t['foo'] = 1
t['bar'] = 2
The following gives an error :
local t = { 'foo' = 1, 'bar' = 2 }
Is there a way to do it similarly to my first code snippet ?
The correct way to write this is either
local t = { foo = 1, bar = 2}
Or, if the keys in your table are not legal identifiers:
local t = { ["one key"] = 1, ["another key"] = 2}
i belive it works a bit better and understandable if you look at it like this
local tablename = {["key"]="value",
["key1"]="value",
...}
finding a result with : tablename.key=value
Tables as dictionaries
Tables can also be used to store information which is not indexed
numerically, or sequentially, as with arrays. These storage types are
sometimes called dictionaries, associative arrays, hashes, or mapping
types. We'll use the term dictionary where an element pair has a key
and a value. The key is used to set and retrieve a value associated
with it. Note that just like arrays we can use the table[key] = value
format to insert elements into the table. A key need not be a number,
it can be a string, or for that matter, nearly any other Lua object
(except for nil or 0/0). Let's construct a table with some key-value
pairs in it:
> t = { apple="green", orange="orange", banana="yellow" }
> for k,v in pairs(t) do print(k,v) end
apple green
orange orange
banana yellow
from : http://lua-users.org/wiki/TablesTutorial
To initialize associative array which has string keys matched by string values, you should use
local petFamilies = {["Bat"]="Cunning",["Bear"]="Tenacity"};
but not
local petFamilies = {["Bat"]=["Cunning"],["Bear"]=["Tenacity"]};

Resources