Jump to content




a quick question about my program


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

#1 tom2018

  • Members
  • 135 posts

Posted 26 October 2012 - 08:52 PM

ok i am making a nice file viewer for an OS i plan to make but when i run it
local files = fs.list("/")
print(files)
it says table: and then a random amount of numbers / letters
how do i make it print the the contents of directory / and not the numbers/letters

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 26 October 2012 - 09:06 PM

You can't really just print a table, haha.

You have to go through the entire thing and print each of its values individually.
for _, file in pairs(files) do
  print(file)
end


#3 tom2018

  • Members
  • 135 posts

Posted 26 October 2012 - 09:08 PM

can you please explain how that code works?

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 26 October 2012 - 09:09 PM

View Posttom2018, on 26 October 2012 - 09:08 PM, said:

can you please explain how that code works?

I was going to ask the exact same question.

EDIT: Did some quick googling:

Spoiler


#5 tom2018

  • Members
  • 135 posts

Posted 26 October 2012 - 09:14 PM

Ok thanks for the help

#6 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 26 October 2012 - 10:14 PM

You could run a for loop, its easier to understand:
for i = 1, #files do -- #files returns the length of the table "files"
  print(files[i])
end

Thats how I first learned to do it, but the above solution is probably better :D/>

#7 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 26 October 2012 - 10:16 PM

You can use either one, really. It's a matter of preference.

#8 ChunLing

  • Members
  • 2,027 posts

Posted 26 October 2012 - 11:49 PM

The iterative for loop is faster than the in pairs, but in pairs doesn't require you to save the table into a separate variable. I've found it's a little harder to screw up with in pairs, which can be a factor.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users