6
votes

I have an external lua table that is structured 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 am trying to use the following lua script to print the exec key/value pair as follows:

function DeepPrint (e)
   if type(e) == "table" then
       for k,v in pairs(e) do 
          if k == "exec" then
            print(k)
            DeepPrint(v)       
          end
       end
   else 
    print(e)
   end
 end


FileStr = "lariatData-sgeT-2012-05-01_2.lua"
Hnd, ErrStr = io.open(FileStr, "r")
if Hnd then
    dofile(FileStr)
    for Str in Hnd:lines() do       
        DeepPrint(sgeT)            
    end
Hnd.close()
else
    print(ErrStr, "\n")
end

Ideally, I would like to print the numerical indices and exec values, such as:

2535047 execVal

However, nothing returns when I run the code. Originally the numerical indices were not enclosed in square brackets, but I added them to enable the file to be read. However, given that they are not sequential, I cannot loop through them like an array, yet I believe that these numerical indices may be the source of my problems. I am not sure what is wrong with the code I have, but it returns nothing. Can anyone recommend how I can fix the code so that I can get the appropriate keys and values to return? Thanks in advance!!!

2

2 Answers

3
votes

Since your table has a fixed structure, you can simply use the syntactical sugar sgeT[key1][key2] to access exec key.

for i, v in pairs( sgeT ) do
  print( i, v[1].exec )
end
2
votes

You are iterating over lines in the file, but you already processed and loaded that file (as a Lua chunk), so it's no longer needed. Just do this:

dofile(FileStr)
DeepPrint(sgeT)

The main issue is that you only call DeepPrint recursively for those keys that have value exec, but none of the keys at the first level do (so nothing gets printed as you didn't go beyond the first level). You probably need to close if before calling DeepPrint:

function DeepPrint (e)
   if type(e) == "table" then
       for k,v in pairs(e) do 
          if k == "exec" then
            print(k)
          end
          DeepPrint(v)       
       end
   else 
    print(e)
   end
 end