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

Help printing data inside { }

Started by kingcoon, 26 December 2012 - 06:33 PM
kingcoon #1
Posted 26 December 2012 - 07:33 PM
Help please, In my program i have stored some login names under the variable "usernames" :
this is the line : username = {"user1", "user2", "user3", "user4"}
after this i try to print the variable( print(username) )but all it shows is "table: 15d69505" when i run it.

Am I doing something wrong or does this just not work?

thanks,

kingcoon
Luanub #2
Posted 26 December 2012 - 07:37 PM
The {} make it a table and the data is stored in indicies of the table. The way you have setup the table it will use standard number indicies, starting with the number 1 and increasing with each additional data set.

Here are a couple of ways to print the data.

print(username[1])
print(username[2])
print(username[3])
print(username[4])

--or
for x=1, #username do
  print(username[x])
end
kingcoon #3
Posted 26 December 2012 - 07:40 PM
thank you
snoble2022 #4
Posted 27 December 2012 - 12:59 PM
Acctually, tables start at 0
BustedEarLobes #5
Posted 27 December 2012 - 01:03 PM
Acctually, tables start at 0

Not in lua. Most other scripting languages (JS, Java, Python, PHP) use 0 but lua starts at one because people new to scripting think that starting a table at 0 is stupid.
Grim Reaper #6
Posted 27 December 2012 - 01:07 PM
Acctually, tables start at 0
The {} make it a table and the data is stored in indicies of the table. The way you have setup the table it will use standard number indicies, starting with the number 1 and increasing with each additional data set.

Here are a couple of ways to print the data.

print(username[1])
print(username[2])
print(username[3])
print(username[4])

--or
for x=1, #username do
  print(username[x])
end


When you create a new table in Lua the indexing starts at 1 and increases with every entry.
However, you can assign a value to any numeric; string; or actual identifier as an index.

Consider the following code:

local t = {"firstEntry", "secondEntry"}
t[-1] = "negativeFirstEntry"

print(t[-1])
It will output:

negativeFirstEntry

Also:

local t = {x = 5, y = 1}
print(t.x .. ' ' .. t.y)
Will display:

5 1

However, the following code:

local t = {x = 5, y = 1}
print(t[1] .. ' '  .. t[2])
Will throw an error because you're trying to concatenate the string ' ' with t[1] and t[2], which are nil values.

Also, luanub, it's usually best to iterate through tables using an iterator function such as 'pairs' or 'ipairs', but it doesn't usually matter all that much.
ChunLing #7
Posted 27 December 2012 - 03:54 PM
It's fastest to iterate with a numeric for loop, and safest to iterate with a for in pairs loop.

But for purposes of explaining the concept to someone new to the idea of tables, focusing just on what you know they already know is "best".
Luanub #8
Posted 27 December 2012 - 04:19 PM
Acctually, tables start at 0
The {} make it a table and the data is stored in indicies of the table. The way you have setup the table it will use standard number indicies, starting with the number 1 and increasing with each additional data set.

Here are a couple of ways to print the data.

print(username[1])
print(username[2])
print(username[3])
print(username[4])

--or
for x=1, #username do
  print(username[x])
end


When you create a new table in Lua the indexing starts at 1 and increases with every entry.
However, you can assign a value to any numeric; string; or actual identifier as an index.

Consider the following code:

local t = {"firstEntry", "secondEntry"}
t[-1] = "negativeFirstEntry"

print(t[-1])
It will output:

negativeFirstEntry

Also:

local t = {x = 5, y = 1}
print(t.x .. ' ' .. t.y)
Will display:

5 1

However, the following code:

local t = {x = 5, y = 1}
print(t[1] .. ' '  .. t[2])
Will throw an error because you're trying to concatenate the string ' ' with t[1] and t[2], which are nil values.

Also, luanub, it's usually best to iterate through tables using an iterator function such as 'pairs' or 'ipairs', but it doesn't usually matter all that much.

Keep in mind in future version of Lua ipairs is being depreciated so in order to iterate through the table as you would have with it you will need to use a numerical for loop, pairs will still be there and will work for tables without standard labeled indices.
ChunLing #9
Posted 27 December 2012 - 04:26 PM
I don't know why they had ipairs in the first place, it's slower than numeric iteration and doesn't do anything different. Grrr. Grrrr I say.