Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在没有外部库的情况下,如何在Lua中实现此循环_Lua_Iterator_Lua Table - Fatal编程技术网

在没有外部库的情况下,如何在Lua中实现此循环

在没有外部库的情况下,如何在Lua中实现此循环,lua,iterator,lua-table,Lua,Iterator,Lua Table,我是Lua的新手,所以请容忍我。我在Lua中有2个csv字符串 a= '1,2,3,4,5' 表示索引 及 行由“:”字符而不是换行符分隔 预期产出 1,this,2,needs,3,to,4,be,5,matched 1,with,2,every,3,single,4,row,5,here 我尝试用下面的代码分别遍历它们 local result= {} local u = unpack or table.unpack for values in string.gmatch(values_

我是Lua的新手,所以请容忍我。我在Lua中有2个csv字符串

a= '1,2,3,4,5'
表示索引 及

行由“:”字符而不是换行符分隔

预期产出

1,this,2,needs,3,to,4,be,5,matched
1,with,2,every,3,single,4,row,5,here
我尝试用下面的代码分别遍历它们

local result= {}
local u = unpack or table.unpack
for values in string.gmatch(values_csv, '([^:]+)') do
    local data = {}
    for column1,column2 in string.gmatch(values, '([^,]+)'),string.gmatch(keys, '([^,]+)') do
        print(column1, column2)
    end
end

出于某种原因,第二个总是零。如果没有外部libs,我在Lua中找不到类似于Python的zip函数。我如何模拟地迭代这两个。感谢您的帮助

Egor解决方案的一个变体,以按照您的要求获得输出:

local keys = '1,2,3,4,5'
local values_csv = 'this,needs,to,be,matched:with,every,single,row,here:some,values,,are,absent:'

for values in values_csv:gmatch'[^:]+' do
   local data = {}
   local keys_iter = keys:gmatch'[^,]+'
   for value_column in (values..','):gmatch'([^,]*),' do
      print(keys_iter(), value_column)
   end
end
a = '1,2,3,4,5'
b = 'this,needs,to,be,matched:with,every,single,row,here:'

for line in b:gmatch '[^:]+' do
  local idx = a:gmatch '%d+'
  local ans = {}
  for v in line:gmatch '[^,]+' do
    ans[#ans+1] = idx()
    ans[#ans+1] = v
  end
  print(table.concat(ans,','))
end

向上投票!谢谢,它给了我一个错误stdin:1:尝试索引一个nil值(全局'values_csv')堆栈回溯:stdin:1:在主块[C]:in?@PirateApp中-我没有看到你的程序。我的代码没有给出这样的错误。
a = '1,2,3,4,5'
b = 'this,needs,to,be,matched:with,every,single,row,here:'

for line in b:gmatch '[^:]+' do
  local idx = a:gmatch '%d+'
  local ans = {}
  for v in line:gmatch '[^,]+' do
    ans[#ans+1] = idx()
    ans[#ans+1] = v
  end
  print(table.concat(ans,','))
end