lua-users home
lua-l archive

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


On Sat, Oct 20, 2012 at 11:21 AM, Hisham <h@hisham.hm> wrote:
> On Sat, Oct 20, 2012 at 12:43 AM, Rena <hyperhacker@gmail.com> wrote:
>> The problem that occurs to me is that print() does a bit of magic to
>> align things nicely to columns (it looks more complex than just
>> appending a tab to everything?) and I'm not sure how that works.
>
> No, it really just runs tostring on all arguments, and then concats
> them with tab and adds a line break. This is all there is to it.
> Here's print reimplemented in Lua:
>
> function print(...)
>    for i = 1, select("#", ...) do
>       if i > 1 then
>          io.stdout:write("\t")
>       end
>       io.stdout:write(tostring(select(i, ...)))
>       end
>    io.stdout:write("\n")
> end
>
> (Note that it has to be done with select() in order to handle nils in
> arguments correctly.)
>
> -- Hisham
>

Ah, that's good to know. I feel like the point of this thread has been
missed though; it wasn't "how do I reimplement print" but "wouldn't it
be interesting if we could access local variables by table indexing
syntax".

-- 
Sent from my Game Boy.