lua Printing specific key/value pairs in table
lua
Solution
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
Problem
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!!!