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

Help with tables

Started by Dejected, 14 December 2014 - 06:23 PM
Dejected #1
Posted 14 December 2014 - 07:23 PM
Can anyone tell me why this works for me:

table = {};
table["a"] = value1;
table["b"] = value2;
etc..
but this doesn't:

table = {"a" = value1, "b" = value2, etc...};

Appreciate the help.
Dragon53535 #2
Posted 14 December 2014 - 07:35 PM

table = {"a" = value1, "b" = value2, etc...};

You're doing it wrong.

tbl = {a = "value1", b = "value2"} --#Don't use the word table for your tables
--#Or you could do
tbl = {["a"] = "value1",["b"] = "value2"}
Dog #3
Posted 14 December 2014 - 07:41 PM
In your second example, you're attempting to set a string ("a" - note the quotes) equal to a variable. What I believe you're trying to do is set the variable 'a' equal to the variable named value1.

Your first example is correct. An equivalent correct example for your second example would be like this…

local myTable = { a = value1, b = value2 , etc...};

You'll notice I also localized the variable assigned to the table (always a good practice - local variables are faster and aren't accessible/changeable from other programs on the same system). Another thing I did was rename the variable from 'table' to 'myTable' - since there is a table api, you don't want to overwrite that.

EDIT :ph34r:/> 'd
Edited on 14 December 2014 - 06:41 PM
Dejected #4
Posted 14 December 2014 - 07:58 PM
I was just using the word table as an example, and I was trying to assign table["a"] to a value, so I purposefully made the a in quotes. Thanks for the help, and dragon's second example worked

tbl = {["a"] = "value1",["b"] = "value2"}
Dragon53535 #5
Posted 14 December 2014 - 08:08 PM
I was just using the word table as an example, and I was trying to assign table["a"] to a value, so I purposefully made the a in quotes. Thanks for the help, and dragon's second example worked

tbl = {["a"] = "value1",["b"] = "value2"}
A little tip when using tables that have string keys that you already know.
If you want to reference tbl["a"] you don't have to use that

tbl.a == "value1"
tbl["b"] == "value2"
Notice though, that you can't use a variable to search for the value, you have to hard code it, so the first is not good if you're having changing key's, however for static keys, such as in this example, you can easily reference them.


Both of my examples should work, as i've used them both interchangeably inside of a single table.

tbl = {["a"] = "value1",b = "value2"}
It's the same, they do the same thing, however one is technically hard coded, and the other you can use a string variable in it's place.
theoriginalbit #6
Posted 14 December 2014 - 11:07 PM
just to expand a little bit with table syntax


--# table creation
local tbl = {
  "value",            --# entries without a key will be stored under an index
  [1] = "value",	  --# keys/indexes can be in [ ]
  [-4] = "value",	 --# it is possible to use 0 or negative indexes with [ ]
  ["key"] = "value",  --# this is the most verbose definition
  key = "value",	  --# and can actually be simplified to this
  key1 = "value",	 --# you can even do numbers, just not at the start, 1key would be a parser error
  ["a key"] = "value",--# any key with non-alphanumeric chars must be done like this
  key = "value";	  --# table entries can be separated by a ; or ,
}

--# table access
print( tbl[1] )	   --# must be used for any index (positive, 0, or negative)
print( tbl["key"] )   --# can be used for keys too
print( tbl.key )	  --# keys can have a shorthand, as long as they're alphanumeric
Edited on 14 December 2014 - 10:09 PM
LtKillPuppy #7
Posted 14 December 2014 - 11:55 PM
Great post, theoriginalbit.

To expand further, on the difference between array tables and associative tables in Lua, and how you use the built-in pairs/ipairs iterators to walk over them:


local array_table = { "one", "two", "three" }
local associative_table = { one=1, two=2, three=3 }
local mixed_table = { "one", "two", three=3 }

print ("length of array=", #array_table)  -- number of items in array table
print ("length of associative_table=", #associative_table) -- zero, because this is not an array
print ("length of mixed_table=", #mixed_table) -- number of items in table that are part of the array (notice: 2, not 3)

print ("ipairs on array:")

for index, value in ipairs (array_table) do  -- ipairs only works with arrays
  print (index, value)
end

print ("ipairs on mixed table:")

for index, value in ipairs (mixed_table) do -- notice how it skips "three"?
  print (index, value)
end

print ("ipairs on associative table:")

for index, value in ipairs (associative_table) do -- Prints nothing!
  print (index, value)
end

print ("pairs on associative table (notice the order):")

for key, value in pairs (associative_table) do -- Returns nothing!
  print (key, value)
end