PtokaX forum

Development Section => Your Developing Problems => Topic started by: Herodes on 07 August, 2004, 18:16:37

Title: Table checker .. (debuger)
Post by: Herodes on 07 August, 2004, 18:16:37
I have this that is also posted in the how to section in Scripting utilities Central ...

tTable = { ["theFirstvalue"] = {
["level"] = "AAA1",
["nrvalues"] = 33300000,
["goodarray"] = { "array A1", "array A2", "array A3", 123 },
},
["theSecond"] = {
["2level"] = "AAA2",
["2nrvalues"] = 3,
["2goodarray"] = { "array B1", "array B2", "array B3", 456},
},
};
function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
local s, e, cmd = strfind(data, "%b<>%s+(%S+)")
SendToAll(cmd)
if strsub(cmd, 1 ,3) == "!do" then
ShowTable(tTable)
end
end
end

function ShowTable(table)
if count == nil then
count = 1
else
count = count + 1
end

for index, val in table do
if type(val) == "table" then
SendToAll("--------> Table/Array STARTS here ...")
ShowTable(val)
SendToAll("<-------- Table/Array ENDS here ...")
elseif type(val) == "string" or type(val) == "number" then
if count ~= nil then
SendToAll(strrep("- ", count*2)..">  "..index.."  >?<  "..val.."    it is a "..type(val))
else
SendToAll( ">  "..index.."  >?<  "..val.."   it is a "..type(val))
end
end
end

end

I need some help to make it display the values like

tTable["theFirstvalue"]["level"] == "AAA1"

Is it ever possible ?
I dont know how to get even the ' ["TheFirstvalue"] ' displayed ... :(

Also I cant figure out a way to show (for index, val) the val if it is a function ... can someone think of something ?

thanks in advance for any input ..
Title:
Post by: plop on 08 August, 2004, 00:47:46
yw in advance. lol
i changed it a bit so it works on the lua command line.
this kind of things you shouldn't try on ptokax because the command line is much handyer.
tTable = {
   ["theFirstvalue"] = {
      ["level"] = "AAA1",
      ["nrvalues"] = 33300000,
      ["goodarray"] = { "array A1", "array A2", "array A3", 123 },
      ["function1"] = function()
         print("function1")
      end,
   },
   ["theSecond"] = {
      ["2level"] = "AAA2",
      ["2nrvalues"] = 3,
      ["2goodarray"] = { "array B1", "array B2", "array B3", 456},
      ["function2"] = function()
         print("function2")
      end,
   },
};

function ShowTable(table, count)
   if count == nil then
      count = 0
   else
      count = count + 1
   end

   for index, val in table do
      if type(val) == "table" then
         print(strrep("--", count*2).."-> Table/Array: "..index.." STARTS here ...")
         ShowTable(val, count)
         print("<-"..strrep("--", count*2).." Table/Array: "..index.." ENDS here ...")
      elseif type(val) == "string" or type(val) == "number" then
         if count ~= nil then
            print(strrep("- ", count*2).."> "..index.." >?< "..val.." it is a "..type(val))
         else
            print( "> "..index.." >?< "..val.." it is a "..type(val))
         end
      elseif type(val) == "function" then
         print(strrep("- ", count*2).."> "..index.." >?< "..tostring(val).." it is a "..type(val))
      end
   end
end

ShowTable(tTable)
plop
Title:
Post by: Herodes on 08 August, 2004, 09:59:34
pro-thank u to plop ...

after his input I manage to add "a mustache and a beard" try this tTable = {

["theFirstvalue"] = {
["level"] = "AAA1",
["nrvalues"] = 33300000,
["goodarray"] = { "array A1", "array A2", "array A3", 123 },
["function1"] = function()
return "function1"
end,
},
["theSecond"] = {
["2level"] = "AAA2",
["2nrvalues"] = 3,
["2goodarray"] = { ["array B1"] = {"a", "b", "c", "d", "e", "f", "g"}, prey = "array B2", ["1"] = function() return "ok" end,  ["3"] = 456},
["function2"] = function()
return "function2"
end,
},
["andanother"] = { "true", "false", "maybe", "could have" , "impossible", "dont think so" },
};


function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
local s, e, cmd = strfind(data, "%b<>%s+(%S+)")
SendToAll(cmd)
if strsub(cmd, 1 ,3) == "!do" then
ShowTable(tTable)
end
end
end


function ShowTable(table, count)
if (count == nil) then
count = 0
else
count = count + 1
end

for index, val in table do
if (type(val) == "table") then
if getn(val) ~= 0 then
SendToAll(strrep("--", count*2).."-> start of ' "..index.." ' ( - array of "..getn(val).." - ) "..strrep("--", count*2))
else
SendToAll(strrep("--", count*2).."-> start of ' "..index.." ' ( - associative "..type(val).." - ) "..strrep("--", count*2))
end
ShowTable(val, count)
SendToAll("<-"..strrep("--", count*2).." end of ' "..index.." ' ( - "..type(val).." - ) "..strrep("--", count*2))
elseif ( (type(val) == "string") or (type(val) == "number") ) then
if (count ~= nil) then
SendToAll(" ? "..strrep("- ", count*2)..">  ( - "..type(val).." - ) \t"..index.." >?< "..val)
else
SendToAll(">  ( - "..type(val).." - ) \t"..index.." >?< "..val)
end
elseif (type(val) == "function") then
SendToAll(" ? "..strrep("- ", count*2).."> ( - "..type(val).." - ) \t"..index.." >?< "..tostring(val))
end
end
end

Theis will give out the following ...
-> start of ' theFirstvalue ' ( - associative table - )
-----> start of ' goodarray ' ( - array of 4 - ) ----
 ? - - - - >  ( - string - ) 1 >?< array A1
 ? - - - - >  ( - string - ) 2 >?< array A2
 ? - - - - >  ( - string - ) 3 >?< array A3
 ? - - - - >  ( - number - ) 4 >?< 123
<----- end of ' goodarray ' ( - table - ) ----
 ? - - > ( - function - ) function1 >?< function: 00EEEF40
 ? - - >  ( - number - ) nrvalues >?< 33300000
 ? - - >  ( - string - ) level >?< AAA1
<- end of ' theFirstvalue ' ( - table - )
-> start of ' andanother ' ( - array of 6 - )
 ? - - >  ( - string - ) 1 >?< true
 ? - - >  ( - string - ) 2 >?< false
 ? - - >  ( - string - ) 3 >?< maybe
 ? - - >  ( - string - ) 4 >?< could have
 ? - - >  ( - string - ) 5 >?< impossible
 ? - - >  ( - string - ) 6 >?< dont think so
<- end of ' andanother ' ( - table - )
-> start of ' theSecond ' ( - associative table - )
 ? - - >  ( - string - ) 2level >?< AAA2
 ? - - > ( - function - ) function2 >?< function: 00EEE508
-----> start of ' 2goodarray ' ( - associative table - ) ----
 ? - - - - > ( - function - ) 1 >?< function: 00EE7690
 ? - - - - >  ( - number - ) 3 >?< 456
---------> start of ' array B1 ' ( - array of 7 - ) --------
 ? - - - - - - >  ( - string - ) 1 >?< a
 ? - - - - - - >  ( - string - ) 2 >?< b
 ? - - - - - - >  ( - string - ) 3 >?< c
 ? - - - - - - >  ( - string - ) 4 >?< d
 ? - - - - - - >  ( - string - ) 5 >?< e
 ? - - - - - - >  ( - string - ) 6 >?< f
 ? - - - - - - >  ( - string - ) 7 >?< g
<--------- end of ' array B1 ' ( - table - ) --------
 ? - - - - >  ( - string - ) prey >?< array B2
<----- end of ' 2goodarray ' ( - table - ) ----
 ? - - >  ( - number - ) 2nrvalues >?< 3
<- end of ' theSecond ' ( - table - )

Now I think it is quite messy ... it doesnt really help to have this so some1 sees a table ... I was wondering if I could get the function: 00EE7690 parts a bit more clear to understand ...
Also , is it possible to Display how is the name of the table we are viewing ? in this case ' tTable' ...
I noted that the tTable["theSecond"]["function2"] appears before tTable["theSecond"]["2nrvalues"] and tTable["theSecond"]["2goodarray"] but not before tTable["theSecond"]["2level"]  ... how does that happen ?
Are functions processed earlier from other types ?
Title:
Post by: plop on 08 August, 2004, 14:44:19
array's are indexed by numbers, so everytime you loop them they appear in the same order.
if you sort them they are re-indexed.
the vallue's of a table are always on the move, it's some weird mix of sorting and checking which is used most/last.
array's are tables as you know and i said that they appear in the same order, the indexing makes them really show in the right order.
if you check freshstuff you can see that i'm using this same trick on a table.
and yes it looks a bit messy, i personaly rather save the table to file.
if tabbed correct it looks really clean (just like you typed it in the script itself).

plop