lua-users home
lua-l archive

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


On Fri, Feb 21, 2014 at 6:43 PM, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great djczaski once stated:
> > On Feb 21, 2014, at 5:59 PM, Sean Conner <sean@conman.org> wrote:
> >
> > It was thus said that the Great René Rebe once stated:
> >>
> >> Instead of doing it right, look for others who do it worse?
> >
> >  I think it's more an investigation of "how do other languages handle
> > this?" than trying to look for worse.
> >
> >  C, using fgets(), would print:
> >
> > hellohellohellohellohello
>
> That's exactly what I was wondering. How do other languages handle
> this situation? I tried Python, Perl, Ruby, and TCL. They all seem to
> support his expectation and what I would have imagined would be
> returned:
>
>     $ python io.py
>     12 helloworld
>     12 helloworld
>     12 helloworld
>
>     $ ruby io.rb
>     12 helloworld
>     12 helloworld
>     12 helloworld
>
>     $ tclsh io.tcl
>     11 helloworld
>     11 helloworld
>     11 helloworld
>
>     $ perl io.pl
>     helloworld
>     helloworld
>     helloworld
>
>     $ lua io.lua
>     15  hellohellohello

  If the leading number is the number of characters in the string, then it
looks like TCL is stripping out the NUL byte entirely, which, depending on
how you look at it, is on par with the Lua version (wrong input) or worse
(corrupted data).

  -spc


It's removing the newline which seems reasonable. Here's the modified code:

    $ tclsh io.tcl 
    11 hello.world
    11 hello.world
    11 hello.world

    $ cat io.tcl 
    set f [open foo]
    while {1} {
        set line [gets $f]
        if {[eof $f]} {
            close $f
            break
        }
        regsub -all {[^\w\.]} $line "." line
        puts "[string length $line] $line"
    }

 I only looked at TCL briefly 15 years ago on my scripting language journey from Perl to TCL to Python to Lua and back to Python.