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

[QUESTION] [LUA] Tables

Started by anonimo182, 11 October 2012 - 01:36 AM
anonimo182 #1
Posted 11 October 2012 - 03:36 AM
How could I set a variable for every file listed by this code:

list = fs.list("programs")
nfiles = #list
term.setCursorPos(1, 3)
for _,file in ipairs(list) do
print(file)
end
or give me a variable for every file and then print it?

Thanks :P/>/>
Luanub #2
Posted 11 October 2012 - 03:39 AM
If it is stored in a table it already has a var. Here's an example on how to print them. This depends on the structure of the table however and may vary slightly.

local list = {} -- declare as table
list = fs.list("programs")

for x=1, #list do
print(list[x])
end

The first record in the table is list[1], and it increments to list[2] etc etc..
Edited on 11 October 2012 - 01:40 AM
anonimo182 #3
Posted 11 October 2012 - 03:59 AM
but each file that is in the table can be stored as a variable, each variable for each file?
PixelToast #4
Posted 11 October 2012 - 04:09 AM
what you want is the global table
usage:

_G[<string>]
will get the variable of the strings name
anonimo182 #5
Posted 11 October 2012 - 03:45 PM
And how can I implement the global table?