-1

I have an external lua file that has a table stored in it that is formatted as follows:

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",
   },
 }

I want to iterate through the table like an array and return the exec key, value pair, and I am completely new to lua and I am using the following script:

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

However, it is returning that the table is nil. What am I doing wrong?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
amber4478
  • 6,433
  • 3
  • 20
  • 17

1 Answers1

1

In continuation of comments above:

-- 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",
   },
 },
}

The above is your file. Then, your Lua program shall be:

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
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • OK I adjusted the numerical indices to include the square brackets and now I get the following error: stdin:6: attempt to concatenate local 'val' (a table value) stack traceback: stdin:6: in main chunk [C]: in ? – amber4478 Apr 15 '13 at 04:13
  • Note, I adjusted the print statement to just return the value and now it is returning the hexadecimal memory location. – amber4478 Apr 15 '13 at 04:20
  • @amber4478 You need to recursively print the table. Use a recurisve function call. You can use `type(val)=="table"` to call the function again. – hjpotter92 Apr 15 '13 at 04:52
  • Would I put this after the print statement? – amber4478 Apr 15 '13 at 04:56
  • That helps a ton but it prints all the values for every key, how do I adjust it just to print certain keys? – amber4478 Apr 15 '13 at 05:20
  • Keep checking for `if k == ` – hjpotter92 Apr 15 '13 at 05:28
  • I adjusted the function as follows: function DeepPrint (e) -- if e is a table, we should iterate over its elements if type(e) == "table" then for k,v in pairs(e) do -- for every element in the table if (k == "exec") then print(k) DeepPrint(v, "\n") -- recursively repeat the same procedure end end else -- if not, we can just print it print(e) end end – amber4478 Apr 15 '13 at 05:32
  • For some reason using the if statement is still returning all the keys and values. – amber4478 Apr 15 '13 at 05:47
  • I think the numerical indices are the problem because if I ask it just to print the values I still get the keys printing as well. It is assuming that the key is the numerical index. – amber4478 Apr 15 '13 at 06:00
  • I added the if statement and I am still printing all the keys and values. However, if I remove the print statement for the keys it still prints them so I think the numerical indices are the problem. ANy thoughts? – amber4478 Apr 15 '13 at 06:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28230/discussion-between-amber4478-and-hjpotter92) – amber4478 Apr 15 '13 at 06:11