在Lua中打印表格 [英] Print table in Lua

查看:239
本文介绍了在Lua中打印表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lua中有一个脚本,我需要打印变量res,但是我不知道该如何打印.我在另一个要打印的功能中得到了该功能的结果

I have a script in Lua and I need to print the variable res, but I do not know how to do this print. I get the result of the function in another function, where I wanted to do print

function parseCSVLine(line) 
  local res = {}
  local pos = 1
  local sep = ','
  while true do 
     local c = string.sub(line,pos,pos)
     if (c == "") then break end
     if (c == '"') then
        -- quoted value (ignore separator within)
        local txt = ""
        repeat
           local startp,endp = string.find(line,'^%b""',pos) -- Digitos
           txt = txt..string.sub(line,startp+1,endp-1)
           pos = endp + 1
           c = string.sub(line,pos,pos) 
           if (c == '"') then txt = txt..'"' end 
           -- check first char AFTER quoted string, if it is another
           -- quoted string without separator, then append it
           -- this is the way to "escape" the quote char in a quote. example:
           --   value1,"blub""blip""boing",value3  will result in blub"blip"boing  for the middle
        until (c ~= '"')
        table.insert(res,txt)
--        assert(c == sep or c == "")
        pos = pos + 1
     else
        -- no quotes used, just look for the first separator
        local startp,endp = string.find(line,sep,pos)
        if (startp) then 
           table.insert(res,string.sub(line,pos,startp-1))
           pos = endp + 1
        else
           -- no separator found -> use rest of string and terminate
           table.insert(res,string.sub(line,pos))
           break
        end 
     end
  end
  return res
end

示例

local result = parseCSVLine(line) 

我想在这里打印结果

推荐答案

res似乎是作为一个列表创建的.所以试试这个:

res in parseCSVLine seems to be created as a list. So try this:

for i,v in ipairs(result) do print(i,v) end

这篇关于在Lua中打印表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆