whats the easy way to dump a table as a string?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

whats the easy way to dump a table as a string?

Sam Roberts-2
For example:

  t = { {fc32="MVER", 13}, NAME="forty", 9 }

Can I convert t to a string in some way that would make it easy to print
the table as a string?

I'm new to lua, so I'm thinking of how ruby can dump data:

  % ruby -e 'h = { "foo" => "bar", 12 => 16, "n" => 2 }; p h'
  {"n"=>2, 12=>16, "foo"=>"bar"}

I don't need the string to be valid input back to lua, just a reasonable
compact representation of the table contents.

I've looked over the ref manual, but I'm not seeing anything that will
help me. Whats the lua way to do this?

Thanks,
Sam

Reply | Threaded
Open this post in threaded view
|

Re: whats the easy way to dump a table as a string?

Matt Campbell-2
Hello Sam:

The book _Programming in Lua_ covers serialization of tables, both with
and without support for cycles.  For your convenience, here's my
serialize function, which is based on the book's example without cycle
support but returns a string instead of writing to the current output
file.  I hope this helps.

function serialize(o)
  local t = type(o)
  if t == "number" then
    return tostring(o)
  elseif t == "string" then
    return string.format("%q", o)
  elseif t == "table" then
    local result = "{"
    local nextIndex = 1
    local first = true
    for k, v in pairs(o) do
      if first then
        first = false
      else
        result = result .. ", "
      end
      if type(k) == "number" and k == nextIndex then
        nextIndex = nextIndex + 1
      else
        if type(k) == "string" and string.find(k, "^[_%a][_%w]*$") then
          result = result .. k
        else
          result = result .. "[" .. serialize(k) .. "]"
        end
        result = result .. " = "
      end
      result = result .. serialize(v)
    end
    result = result .. "}"
    return result
  else
    return tostring(o)
  end
end

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"

Reply | Threaded
Open this post in threaded view
|

Re: whats the easy way to dump a table as a string?

Sam Roberts-2
On Fri, Apr 07, 2006 at 06:34:26PM -0500, Matt Campbell wrote:
> Hello Sam:
>
> The book _Programming in Lua_ covers serialization of tables, both with
> and without support for cycles.  For your convenience, here's my
> serialize function, which is based on the book's example without cycle
> support but returns a string instead of writing to the current output
> file.  I hope this helps.

Great, thank you.

That sounds like a good book to have.

Sam

Reply | Threaded
Open this post in threaded view
|

Re: whats the easy way to dump a table as a string?

Anders Bergh-2
It's available online, check it out: http://lua.org/pil/

Anders

On 4/8/06, Sam Roberts <[hidden email]> wrote:

> On Fri, Apr 07, 2006 at 06:34:26PM -0500, Matt Campbell wrote:
> > Hello Sam:
> >
> > The book _Programming in Lua_ covers serialization of tables, both with
> > and without support for cycles.  For your convenience, here's my
> > serialize function, which is based on the book's example without cycle
> > support but returns a string instead of writing to the current output
> > file.  I hope this helps.
>
> Great, thank you.
>
> That sounds like a good book to have.
>
> Sam
>
>
Reply | Threaded
Open this post in threaded view
|

Re: whats the easy way to dump a table as a string?

Aaron Brown
In reply to this post by Sam Roberts-2
Sam Roberts wrote:

> Can I convert t to a string in some way that would make it
> easy to print the table as a string?

If you don't care about nested tables, this can be done in a
one-liner:


  > t = {foo = "bar", [12] = 16, [{}] = {}}
  > table.foreach(t, print)
  12      16
  table: 0xa06bb08        table: 0xa06c038
  foo     bar

--
Aaron