-1

我有一个外部 lua 文件,其中存储了一个表格,其格式如下:

sgeT = {
    2535047 = {
    {
       ["account"] = "TG-MCB110105",
       ["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
       ["execEpoch"] = 1305825864,
       ["execModify"] = "Thu May 19 12:24:24 2011",
       ["execType"] = "user:binary",
       ["jobID"] = "2535047",
       ["numCores"] = "128",
       ["numNodes"] = "8",
       pkgT = {
       },
       ["runTime"] = "65125",
       ["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
       sizeT = {
          ["bss"] = "104552",
          ["data"] = "192168",
          ["text"] = "10650813",
      },
      ["startEpoch"] = "1335843433",
      ["startTime"] = "Mon Apr 30 22:37:13 2012",
      ["user"] = "guo",
   },
 },
 2535094 = {
 {
     ["account"] = "TG-MCB110105",
     ["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
     ["execEpoch"] = 1305825864,
     ["execModify"] = "Thu May 19 12:24:24 2011",
     ["execType"] = "user:binary",
     ["jobID"] = "2535094",
     ["numCores"] = "128",
     ["numNodes"] = "8",
     pkgT = {
     },
     ["runTime"] = "81635",
     ["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
     sizeT = {
         ["bss"] = "104552",
         ["data"] = "192168",
         ["text"] = "10650813",
     },
     ["startEpoch"] = "1335823028",
     ["startTime"] = "Mon Apr 30 16:57:08 2012",
     ["user"] = "guo",
   },
 }

我想像数组一样遍历表并返回 exec 键、值对,我对 lua 完全陌生,我正在使用以下脚本:

FileStr = "lariatData-sgeT-2012-05-31.lua"
Hnd, ErrStd = io.open(FileStr, "r")
myTable = loadTable(FileStr)
if Hnd then
for Str in Hnd:lines() do
    print(Str, "\n")
    for exec, val in pairs(myTable) do
        print(exec.." "..val, "\n")
    end
end
Hnd.close()
else
    print(ErrStr, "\n")
end

但是,它返回该表为零。我究竟做错了什么?

4

1 回答 1

1

继续上面的评论:

-- Notice that I've used `[2535047]`
sgeT = {
    [2535047] = {
    {
       ["account"] = "TG-MCB110105",
       ["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
       ["execEpoch"] = 1305825864,
       ["execModify"] = "Thu May 19 12:24:24 2011",
       ["execType"] = "user:binary",
       ["jobID"] = "2535047",
       ["numCores"] = "128",
       ["numNodes"] = "8",
       pkgT = {
       },
       ["runTime"] = "65125",
       ["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
       sizeT = {
          ["bss"] = "104552",
          ["data"] = "192168",
          ["text"] = "10650813",
      },
      ["startEpoch"] = "1335843433",
      ["startTime"] = "Mon Apr 30 22:37:13 2012",
      ["user"] = "guo",
   },
 },
}

以上是你的文件。那么,你的 Lua 程序应该是:

FileStr = "lariatData-sgeT-2012-05-31.lua"
Hnd, ErrStr = io.open(FileStr, "r")
if Hnd then
    dofile(FileStr)
    for Str in Hnd:lines() do
        print(Str, "\n")
        for exec, val in pairs(sgeT) do
            print(exec.." "..val, "\n")
        end
    end
    Hnd.close()
else
    print(ErrStr, "\n")
end
于 2013-04-15T04:04:01.203 回答