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

Turning a Table into A string

Started by Brod8362, 06 March 2016 - 03:49 PM
Brod8362 #1
Posted 06 March 2016 - 04:49 PM
Hey.
Simple issue here, I don't work with tables since I'm a novice and have almost no idea what I am doing.

All I am trying to do is print tables that are obtained via fs.list() and print them into a nice list.

How would I do this?
Thanks!
doublequestionmark #2
Posted 06 March 2016 - 05:06 PM

for file in fs.list() do
	print(file)
end
Edited on 06 March 2016 - 04:06 PM
Brod8362 #3
Posted 06 March 2016 - 05:11 PM

for file in fs.list() do
	print(file)
end

This didn't work. It still is expecting a string, which it isn't getting.
doublequestionmark #4
Posted 06 March 2016 - 05:16 PM
ok, i get whats happening. fs.list() requires and argument of the directory you want it to list.
in simple terms, you have to tell it what to list.

for example, if you want to get all the files in the root directory, '/', you would use:
fs.list('/')
Bomb Bloke #5
Posted 06 March 2016 - 10:24 PM
That, and the "for" loop itself expects a function, such as the one returned by ipairs.

for _, file in ipairs( fs.list( "/" ) ) do
  print( file )
end

There's also textutils.serialise() and textutils.tabulate().
Edited on 06 March 2016 - 09:44 PM
Creator #6
Posted 06 March 2016 - 10:42 PM
This should not work either. The generic for loop requires a function, which would be returned by fs.list. But this one returns a table. The way to do it is:


for i,file in pairs(fs.list(path)) do
  print(file)
end

Ninja'd. And that is what I get for not refreshing the page.
Edited on 06 March 2016 - 09:42 PM