139 posts
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
1688 posts
Location
'MURICA
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
139 posts
Posted 26 October 2012 - 10:08 PM
can you please explain how that code works?
2088 posts
Location
South Africa
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:
Spoiler
They 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
139 posts
Posted 26 October 2012 - 10:14 PM
Ok thanks for the help
200 posts
Location
Scotland
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/>/>
1688 posts
Location
'MURICA
Posted 26 October 2012 - 11:16 PM
You can use either one, really. It's a matter of preference.
2005 posts
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.