This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jessechisel126's profile picture

How Do I Read Data From Tables?

Started by jessechisel126, 26 July 2013 - 04:47 AM
jessechisel126 #1
Posted 26 July 2013 - 06:47 AM
Hello all,

I have a little bit of experience with C and Python programming languages but I am just now taking up Lua for use with this mod and peripheral mods. I'm unfamiliar with the "table" data type and I am having trouble accessing data that has been output to a table. I've tried what I know about arrays to access it, since I've read that they're similar to tables, to no avail. I've found the reference manual for Lua at www.lua.org but it's a little too dense for me to effectively find out what I am trying to find out (also, the word table shows up almost 400 times).

What I do know is that in code I have used from elsewhere, a table was accessed as follows:


...
greader = peripheral.wrap("right")
data = greader.get()
while not data["Full Energy"] do
...

The get() function is from MiscPeripherals and is essentially just supposed to output the data it gets to a table. So, "data" in this case is a table. Trying to access this table to print out the status of a REC in interactive mode gives me things like this:


lua> data = peripheral.wrap("right").get
lua> data
table: 56cfad29
lua> data[0]
nil
lua> data[1]
nil
lua> data["Full Energy"]
false
lua> data["No Energy"]
true

So from what I can deduce things like "No Energy" and "Full Energy" act like indices. Is there a way to check out what all of the indices of a table are (if my guess is right)? Also, can tables only hold boolean variables, or can they be an amalgamation of types (like lists in Python)?
LBPHacker #2
Posted 26 July 2013 - 06:57 AM
Tables can hold anything, even other tables. The indices can be indexed by any type - number, string, even function. You can list the indices of a table using this:
for key, value in pairs(tbl) do print(tostring(key) .. ": " .. tostring(value)) end

Multidimensional tables: They are just tables containing other tables. Like
local a = {z = {0, 1}, ["y"] = {2, 3}}
print(a["z"][1]) -- will print 0
print(a.y[2]) -- will print 3

If a string index (like ["y"]) has no spaces in it (so no "a b c" but "abc") (so no special characters), it can be indexed like this: a.y. The basic rule for variable names applies to the string indices, so practically anything beginning with a letter or an underscore can be indexed like that. a["_something"] is the same as a._something.
jessechisel126 #3
Posted 26 July 2013 - 07:02 AM
Tables can hold anything, even other tables. The indices can be indexed by any type - number, string, even function. You can list the indices of a table using this:
for key, value in pairs(tbl) do print(tostring(key) .. ": " .. tostring(value)) end

Multidimensional tables: They are just tables containing other tables. Like
local a = {z = {0, 1}, y = {2, 3}}
print(a["z"][1]) -- will print 0
print(a.y[2]) -- will print 3

Thanks for the help, exactly what I needed!
Engineer #4
Posted 26 July 2013 - 07:04 AM
A table is a two part array, a map you could say. We have the key and the value in this map.
If you want to have a value, you need to know what the key is. Here is an example:

local t = { -- Open a table
  [1] --[[ Your first key ]] = "test" -- Your value
} -- Close the table

-- So now if we want to acces the value, you can simply do this:
print( t[1] ) -- So now we are referring to test, and it will print test for us.

-- Lets make another table
local x = {
   ["This is a key!"] = 5, -- Note the comma if you want multiple values and keys !
   ["ThisIsAnotherKey"] = 3
}

-- Now if we want to print the value 5, we do this:
print( x["This is a key!"] ) -- prints 3

-- Now we have two options for the other key. They are the same, but it will only work if the string is one word.
print( x["ThisIsAnotherKey"] ) -- prints 3
print( x.ThisIsAnotherKey ) -- prints also 3

So a table alway has an index, but if you dont know those,use the loop my good friend LBPHacker have shown you.
jessechisel126 #5
Posted 26 July 2013 - 07:09 AM
A table is a two part array, a map you could say. We have the key and the value in this map.
If you want to have a value, you need to know what the key is. Here is an example:

local t = { -- Open a table
  [1] --[[ Your first key ]] = "test" -- Your value
} -- Close the table

-- So now if we want to acces the value, you can simply do this:
print( t[1] ) -- So now we are referring to test, and it will print test for us.

-- Lets make another table
local x = {
   ["This is a key!"] = 5, -- Note the comma if you want multiple values and keys !
   ["ThisIsAnotherKey"] = 3
}

-- Now if we want to print the value 5, we do this:
print( x["This is a key!"] ) -- prints 3

-- Now we have two options for the other key. They are the same, but it will only work if the string is one word.
print( x["ThisIsAnotherKey"] ) -- prints 3
print( x.ThisIsAnotherKey ) -- prints also 3

So a table alway has an index, but if you dont know those,use the loop my good friend LBPHacker have shown you.

That really helps to make sense of it. From what I've seen so far, tables are quite a convenient data structure; it's like all of the good things about arrays and structs wrapped into one!