是否可以为表中的所有内容创建一个带有字符串的变量?

时间:2020-06-24 16:46:56

标签: lua computercraft

只是想知道,因为我没有找到解决方案。可以肯定,因为我是新手c:谢谢您的帮助。

编辑:为便于解释,我将用代码进行一些解释。

local FileList = fs.list("") --Makes a Table with all the files and directories available (as strings) on the PC it's running on (Inside of a mod called computercraft for minecraft)

for _, file in ipairs(FileList) do 
    --Here I need a function which assigns every string from the table to a variable, just for the explanation I'll call it unknown.function
   
    unknown.function
end 

while true do
    print(a) --a is for example one of the different variables made from the "unknown.function" 
    sleep(1)
end

2 个答案:

答案 0 :(得分:1)

喜欢这个吗?

AllFiles = {}

function Crazyfunction(file)

 AllFiles[table.getn(AllFiles)+1] = file
 
end

local FileList = fs.list("")

for _, file in ipairs(FileList) do 
    Crazyfunction(file)
end 

while true do
    print(AllFiles[NUMBE_OF_FILE_HERE])
    sleep(1)
end

答案 1 :(得分:0)

您的意思是这样吗?

local FileList = fs.list("")

for _, file in ipairs(FileList) do 
    -- file is the variable which contains a string from the table.
    print(file)
    sleep(1)
end 

您想要的是...您的循环已经在做什么。您只是无缘无故地添加了一个额外的循环。

相关问题