lua-users home
lua-l archive

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

A printable table is one that has a `__tostring` metamethod.  In a
well-designed object-oriented application, such a metamethod would be
provided as a matter of course.  In many cases, though, it is simply
not worth the effort.

For those cases, I've written a one-size-fits-all function that can be
used for that purpose.  It uses four reserved keys: `_sep`, `_fmt`,
`_kv` and `_post`, with inheritance via the `__index` metamethod to
achieve flexibility.


    ptab = require "printable"
--> {1,2,{3,4},msg="hello"}
    A = {{1,2,3},{4,5,6}}
    latexmatrix = {{_sep=' \\\\\n',_post='%'},' & '}
    print (ptab(A,latexmatrix))
--> 1 & 2 & 3 \\
--> 4 & 5 & 6