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

[solved] Trouble printing from tables

Started by Rsstn, 04 August 2012 - 10:51 AM
Rsstn #1
Posted 04 August 2012 - 12:51 PM
I'm having some trouble when trying to print from tables. The code that I'm using is:


print(table[1])

This does work, but after printing the data held in row 1 of the table, it prints '1'. Even when printing rows 2/3/4/etc. of the table, every time it prints '1' after printing the table data.
Am I doing something wrong here?
Help appreciated!
Luanub #2
Posted 04 August 2012 - 01:21 PM
That should work fine. What does the rest of the code look like? The print command is correct.

Try doing this.

local tTable = {}
tTable[1] = 1
tTable[2] = 2

print(tTable[1])
print(tTable[2])
Kolpa #3
Posted 04 August 2012 - 01:21 PM
i don't even see how you are creating the table how should i be able to help you
Rsstn #4
Posted 04 August 2012 - 01:28 PM
Sorry. I'm creating the table like this:

tTable={}

and adding data to it like this:

tTable[1] = "hello"
tTable[2] = "hello2"

then when I print with this:

print(tTable[1])

the output is this:

hello
1

At the moment, I'm only using the lua program to test out the commands before I tried using them in a program - I've never tried to print a table before, so I'm just trying things out.
Luanub #5
Posted 04 August 2012 - 01:35 PM
Are you doing this from the lua prompt? If so thats probably your problem. Put them in a program and it will work fine.

Should look something like

local tTable={}

tTable[1] = "hello"
tTable[2] = "hello2"

for x=1, #tTable do
print(tTable[x])
end

Instead of the for loop you can also just print each portion of the table like

print(tTable[1])
print(tTable[2])
Edited on 04 August 2012 - 11:42 AM
Kolpa #6
Posted 04 August 2012 - 01:38 PM
ignore me damn ninjas
KaoS #7
Posted 04 August 2012 - 02:19 PM
dude no matter what you print it will show a 1 under it if you use the lua interpreter because it returns the output of the command you run, print returns a '1' value if it succeeds, in a program it won't show that
MysticT #8
Posted 04 August 2012 - 06:01 PM
dude no matter what you print it will show a 1 under it if you use the lua interpreter because it returns the output of the command you run, print returns a '1' value if it succeeds, in a program it won't show that
Actually print returns the number of lines used.
KaoS #9
Posted 04 August 2012 - 08:45 PM
ah, I stand corrected
Rsstn #10
Posted 04 August 2012 - 09:27 PM
Ah, ok, thanks. I thought I was being clever by using the command prompt to test my code before I wrote a whole program full of errors!
Somehow I missed half of the information on tables when learning lua, and wanted to test what I was doing with them!
Sorry about the lack of info on my first post, too! I was in a bit of a hurry when posting it, and didn't think about it much.
Thanks again!