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

a quick question about my program

Started by tom2018, 26 October 2012 - 07:52 PM
tom2018 #1
Posted 26 October 2012 - 09: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
Kingdaro #2
Posted 26 October 2012 - 10: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
tom2018 #3
Posted 26 October 2012 - 10:08 PM
can you please explain how that code works?
remiX #4
Posted 26 October 2012 - 10:09 PM
can you please explain how that code works?

I was going to ask the exact same question.

EDIT: Did some quick googling:

SpoilerThey loop through a table

tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) -- K = apples, oranges V = 2, 3
end
tom2018 #5
Posted 26 October 2012 - 10:14 PM
Ok thanks for the help
darkrising #6
Posted 26 October 2012 - 11: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/>/>
Kingdaro #7
Posted 26 October 2012 - 11:16 PM
You can use either one, really. It's a matter of preference.
ChunLing #8
Posted 27 October 2012 - 12:49 AM
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.