lua-users home
lua-l archive

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


On Feb 28, 2015 4:00 PM, "Nagaev Boris" <bnagaev@gmail.com> wrote:
>
> On Sat, Feb 28, 2015 at 8:30 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> > 2015-02-28 21:44 GMT+02:00 Nagaev Boris <bnagaev@gmail.com>:
> >
> >>
> >> Is this true, that io.read("*n") should be used if only input is
> >> guaranteed to be a number?
> >
> > Not at all. You can use it to find out whether the input starts with
> > a number. You should always test the return value. If nil, you
> > can re-read the input with a more forgiving format.
> >
>
> I've understood why io.read("*n") behaves so:
>
> >  io.read("*n")
> hello
> nil
> >  io.read()
> "hello"
> >
>
> So, if the input is not a number, I can get it as a string.
> Thank you for the explanation!
>

Right. It helps to remember that stdin is an input stream, just like a file. So if io.read('*n') doesn't remove any characters from the input buffer, then the next io.read() will see those same characters.