Tables

since everything in Lua more or less seem to make use of tables, i’ll have to dive into this pool and test things out

instant gratification, you know :)

here’s my progress so far:

  1. table constructors are the curly braces { }
  
TheSupremeTable = {}  
  
  1. a table can be empty but still be a table (?)
  2. tables can have keys and values that are connected to each other somehow
  3. when i experiment it seems that i can put virtually anything in a table:
  
AnotherTable = {  
 ["msg"] = "This is a message",   
 ["the_meaning_of_life"] = 42  
}   
  
print(AnotherTable["msg"], ": the meaning of life is ", AnotherTable["the_meaning_of_life"])  
  

will print out “This is a message: the meaning of life is 42”

note: i had some troubles with this at first because i forgot the quotation marks in the keys (for exampe i wrote [msg] instead of [“msg”])

  1. it seems there is an alternative way to access stuff in tables, maybe this is more common for renoise scripts:
  
Table2 = {key1 = "value1", key2 = "value2", key3 = "value3"}  
str = Table2.key1  
print(str)  
  

will print out “value1”

i suspect at this point that this is how renoise’s api stores all the stuff as well, like: a table in a table in a table in a table

so that when i see this constant:

  
renoise.InstrumentMidiOutputProperties.TYPE_EXTERNAL  
  

it’s actually in a table called “InstrumentMidiOutputProperties”
which in turn is in a table called “renoise”

is this correct?

  1. the above way of having tables inside tables inside tables is called having “nested tables” (what would the verb be? something like “look mom, i’m nesting tables over here”?)

like so:

  
Table1 = {Table2 = {Table3 = {Table4 = {1, 2, 3, 4}}}}  
str = Table1.Table2.Table3.Table4[1]  
print(str)  
  

will print the first value in Table4, which is 1

to be continued… :)

nice explanation .xrns

from what i’ve gathered, LUA tables are similar to what most programming languages call ‘lists’ or ‘arrays’.

Array: {“a”,“b”,“c”}
Associative Array (the so-called key-value pair): {1 = “a”, 2 = “b”, 3 = “c”} - sometimes also {1 => “a”, 2 => “b”, 3 => “c”} - not sure which is the LUA way…
Nested Arrays: {{1,“a”},{2,“b”},{3,“c”},{4,{“def”,“ghi”}}}

From the LUA manual: http://www.lua.org/pil/2.5.html

thanks rhowaldt :)

i get the overall impression that tables in lua are really multifaceted and flexible, and that there are many optional ways to do the same thing

i expect this flexibility to confuse me on the road, but on the other hand i expect it to have limits

now, i just noticed that the nil value works as a kind of eliminator:

  
t = {key1 = "value1", key2 = "value2", key3 = "value3"}  
  
t.key3 = nil  
  
print(t.key1, t.key2, t.key3)  
  

will print “value1 value2 nil”

i can also add or edit existing values:

  
t = {key1 = "value1", key2 = "value2", key3 = "value3"}  
  
t.key1 = "changing value1 to a new value1"  
t.key4 = "adding a new key-value pair to the table"  
  
print(t.key1 .. " | " .. t.key2 .. " | " .. t.key3 .. " | " .. t.key4)  
  

will print “changing value1 to a new value1 | value2 | value3 | adding a new key-value pair to the table”

hmmm… note to self: i need to investigate how to make a for-loop for printing out all the stuff in a table

nice, i just discovered the ipairs… (sounds almost like ipad):

  
t = {}  
  
for i = 1, 1000 do  
 t[i] = i  
end  
  
for i, t in ipairs(t) do  
 print(i .. "th value is now " .. t)  
end  
  

will fill table t with one thousand values ranging between 1-1000 and print “1th value is now 1” … all the way down to “1000th value is now 1000”

i assume this will come in handy later on, when i want to search through pattern data and such :)

Not quite yet got the hang of ipairs but discovered something about LUA Tables that seems quite strange to me!

Create a table, add some data to it, create a second variable and tell it it is the same as the table.

This will point both variables at the same table, not create two tables containing the same data which can be edited separately.

From the PIL pdf:

 a = {}  
 a["x"] = 10  
 b = a -- `b' refers to the same table as `a'  
 print(b["x"]) --> 10  
 b["x"] = 20  
 print(a["x"]) --> 20  

Not sure how you copy a table so both can be edited without affecting the other as of yet though.

I’m guessing “ipairs” means “integer pairs”, because you are working with a table organized by numerical order
“pairs” on the other hand is used for traversing associate arrays (tables organized by string value)

You also have ripairs (reverse ipairs) which is useful if you are doing something that remove entries from the table as you are processing it (try looping through a table from the beginning while removing entries? It won’t work).

It should be as simple as copying the table - quoting from the Lua Standard API:

  
  
-- Copy the metatable and all first level elements of the given table into a  
-- new table. Use table.rcopy to do a recursive copy of all elements  
table.copy(t) -> [table]  
  
-- Deeply copy the metatable and all elements of the given table recursively  
-- into a new table - create a clone with unique references.  
table.rcopy(t) -> [table]  
  

Not actually trying to use them for anything constructive yet (hence it being in the Beginners Sandbox), as I said just trying to get my head around the whole paradigm, but thanks for the reply. Got it to work although had to check the API to get a grip of the syntax compared to how you presented it.

b_table = table.rcopy(a_table) – is how I managed to use it :)