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.

Sample:

    ptab = require "printable"
    S=ptab{msg='hello',1,2,{3,4}}
    print(S)
--> {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

<https://sites.google.com/a/aims.ac.za/experimental-mathematics/lua>

Dirk