Jump to content




[solved] Trouble printing from tables


  • You cannot reply to this topic
9 replies to this topic

#1 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 04 August 2012 - 11:51 AM

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!

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 04 August 2012 - 12: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])


#3 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 04 August 2012 - 12:21 PM

i don't even see how you are creating the table how should i be able to help you

#4 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 04 August 2012 - 12: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.

#5 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 04 August 2012 - 12: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 by luanub, 04 August 2012 - 12:42 PM.


#6 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 04 August 2012 - 12:38 PM

ignore me damn ninjas

#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 04 August 2012 - 01: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

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 04 August 2012 - 05:01 PM

View PostKaoS, on 04 August 2012 - 01:19 PM, said:

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.

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 04 August 2012 - 07:45 PM

ah, I stand corrected

#10 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 04 August 2012 - 08: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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users