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

[Question] Quick Array Question

Started by civilwargeeky, 04 November 2012 - 12:45 PM
civilwargeeky #1
Posted 04 November 2012 - 01:45 PM
Does saying t = {}
the same as saying t = {nil,nil,nil}
Cruor #2
Posted 04 November 2012 - 01:49 PM
In that example, it is the same thing. But if you were to do t = {nil, nil, nil, 1, 2, 3} then the 1 would be the 4. index(obviously :D/>/>) instead of being the first when done like t = {1, 2, 3}
Pharap #3
Posted 04 November 2012 - 02:29 PM
Tables are different from arrays in other languages, they are less strict and allow for multiple data types, including tables and functions.

Tables are also sets of key-value pairs, meaning that if you make a statement like
 t[5]="hello" 
you are setting key 5 of the table to the value of "hello". Any Key that isn't set is automatically nil, so nil is technically the default value of every key.

Therefore, calling
 t={nil,nil,nil}
is essentially the same as calling:
t={} t[1]=nil t[2]=nil t[3]=nil
but since t={} makes all the values in the table nil by default, they are again, essentially the same thing, so
t={nil,nil,nil}
is redundant, as well as
 t={} t[1]=nil
etc

However, if you were to call
t={nil,nil,nil,"hello"}
, it would be the same as calling
t={} t[1]=nil t[2]=nil t[3]=nil t[4]="hello"
thus making it different, however calling
t={} t[4]="hello"
would be a much better method as it is shorter and achieves the same result.

And that is basic table assignment in a (wal)nutshell.