[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Arithmetic on strings
- From: Patrick Rapin <toupie300@...>
- Date: Sun, 8 Jul 2012 00:12:11 +0200
I also would vote for removal of the coercion in Lua 6.0 (not 5.3,
because such a big change requires a major version number increment).
Now concerning this example :
> for n in grade:gmatch("%d+") do s=s+tonumber(n); k=k+1 end
In the past I have been surprised that the "(%d+)" capture pattern
returns a string and not a number.
This is quite logical from the point of view of the string library
implementation, but it is not for end user whose intention is to
capture "a number".
That behavior could not be changed because the capture could be
something like "(%a%d+)" which does not match a valid number, although
there are digits in it.
I would love if there existed a special pattern, let's say %n, which
would match a valid number representation and capture that number.
The above %d+ example only matches decimal unsigned integers.
If you need to match all forms of numbers (floating point with or
without exponent part, signed integers, hexadecimal values), the
pattern becomes very complex if at all possible. The proposed %n
pattern is supposed to support all of them. Internally, the logic for
the feature is already implemented in Lua because this is exactly what
tonumber currently does !