lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I wrote a nice pretty-printer for repl usage that has been very useful
to me.  It's at
https://github.com/creationix/luvit/blob/master/lib/utils.lua

It's part of my luvit runtime, but that particular file can be used
from any lua environment standalone.  Just swap the comments on the
return and the prints below them to see it in action.

http://creationix.com/dump.png

-Tim Caswell

On Tue, Oct 11, 2011 at 7:27 AM, Tomas Guisasola Gorham
<tomas@tecgraf.puc-rio.br> wrote:
> On Tue, 11 Oct 2011, Xavier Wang wrote:
>
>> hi list :)
>>
>> In Lua, to print a table is some how difficult: if you print it
>> direct, a useless string of table: address will print, use
>> table.concat, you can print a sequence easily:
>>
>> local t = {1,2,3,4,5}
>> print(t) --> table: xxxxxxxx
>> print(table.concat(t, ",")) --> 1,2,3,4,5
>>
>> but you can not use this way to print a hash table:
>>
>> local t = {a:1, b:2, c:3}
>> print(table.concat(t, ',')) --> empty line
>>
>> some, maybe table.concat can accept a second sep:
>> print(table.concat(t, ':', ',') --> a:1,b:2,c:3
>
>        Take a look at fullconcat() from Dado's table.extra module:
>
> http://www.ccpa.puc-rio.br/software/dado/luadoc/modules/table.extra.html#fullconcat
>
>        For your example:
>
>> fullconcat = require"table.extra".fullconcat
>> t = { a = 1, b = 2, c = 3 }
>> =fullconcat(t)
>
> a=1,b=2,c=3
>>
>> =fullconcat(t, ':', ',')
>
> a:1,b:2,c:3
>
>        Regards,
>                Tomás