lua-users home
lua-l archive

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



On 14-Sep-04, at 4:34 PM, Asko Kauppi wrote:


Would it be hard to make 'string.find()' return true numbers (not strings with numerical contents, as now):

What do you think - would it get any votes?

Not from me :)

First of all, there are many strings of digits which are not "numbers" -- id numbers, telephone numbers, postal codes (in some countries), credit card numbers, etc. Note that you cannot reliably round-trip such a sequence -- it may have too many digits for the precision of Number, or it may have leading zeros. So getting a number instead of the string loses information.

Furthermore, there are many numbers which are not simply sequences of digits -- negative numbers, floating point numbers, etc. The patterns to recognise these are complicated; it is actually easier to just match the possible character sequence and then verify with tonumber().

I wouldn't object to some % escape that used the c library's numeric recognition algorithm and returned a real number -- although I'm not sure that it would be worthwhile -- but magic behaviour on (%d+) is not a good idea, imho. (And also tricky to check for while parsing the pattern string.)

Rici