Nmap Development mailing list archives
Re: [Lua] printing a table
From: "Patrick Donnelly" <batrick.donnelly () gmail com>
Date: Sat, 22 Nov 2008 22:25:22 -0700
On Sat, Nov 22, 2008 at 5:51 PM, Ron <ron () skullsecurity net> wrote:
Hi all,
The newer methods I'm writing are modelled after the interface that
Microsoft uses for the RPC calls. Unfortunately, this often means that
the important data is 3 or 4 structs down. I was giving myself a
headache trying to remember where everything goes, so I threw together a
quick function to print out everything in a table:
--
---Converts an arbitrary data type into a string. Will recursively convert
-- tables.
--
--@param data The data to convert.
--@param indent (optional) The number of times to indent the line. Default
-- is 0.
--@return A string representation of a data, will be one or more full
lines.
function to_string(data, indent)
local str = ""
if(indent == nil) then
indent = 0
end
-- Check the type
if(type(data) == "string") then
str = str .. (" "):rep(indent) .. data .. "\n"
elseif(type(data) == "number") then
str = str .. (" "):rep(indent) .. data .. "\n"
elseif(type(data) == "boolean") then
if(data == true) then
str = str .. "true"
else
str = str .. "false"
end
elseif(type(data) == "table") then
local i, v
for i, v in pairs(data) do
-- Check for a table in a table
if(type(v) == "table") then
str = str .. (" "):rep(indent) .. i .. ":\n"
str = str .. to_string(v, indent + 2)
else
str = str .. (" "):rep(indent) .. i .. ": " ..
to_string(v, 0)
end
end
else
print_debug(1, "Error: unknown data type: %s", type(data))
end
return str
end
--
Does anybody know if there's an easier (or built-in) way to do this?
I put this code in stdnse for now, on my branch at least.
This isn't really something needed for a script to do its job. So putting it in the main branch is not something I would recommend. Also, you may want to become familiar with the Lua wiki where you will find a lot of useful code. For example, [1] has code to print a table [1] http://lua-users.org/wiki/TableSerialization Cheers, -- -Patrick Donnelly "One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say." -Will Durant _______________________________________________ Sent through the nmap-dev mailing list http://cgi.insecure.org/mailman/listinfo/nmap-dev Archived at http://SecLists.Org
Current thread:
- [Lua] printing a table Ron (Nov 22)
- Re: [Lua] printing a table Patrick Donnelly (Nov 22)
- Re: [Lua] printing a table Ron (Nov 22)
- Re: [Lua] printing a table Ron (Nov 22)
- Re: [Lua] printing a table Kris Katterjohn (Nov 23)
- Re: [Lua] printing a table Ron (Nov 22)
- Re: [Lua] printing a table Patrick Donnelly (Nov 22)
