jv@mh.nl (Johan Vromans) (06/24/91)
Perl 4.010, System Ultrix/RISC, GCC compiled. Exhibit 1: $a = "1a"; $b = $a + 1; I would have expected an error (or at least a warning). Exhibit 2: $a = "1a"; $a++; IMHO, the correct value for $a should now be "1b", not 2. Johan -- Johan Vromans jv@mh.nl via internet backbones Multihouse Automatisering bv uucp: ..!{uunet,hp4nl}!mh.nl!jv Doesburgweg 7, 2803 PL Gouda, The Netherlands phone/fax: +31 1820 62911/62500 ------------------------ "Arms are made for hugging" -------------------------
tchrist@convex.COM (Tom Christiansen) (06/24/91)
From the keyboard of Johan Vromans <jv@mh.nl>: :Perl 4.010, System Ultrix/RISC, GCC compiled. : :Exhibit 1: : : $a = "1a"; : $b = $a + 1; : :I would have expected an error (or at least a warning). No, it's always done that -- atoi()d a for the number part. :Exhibit 2: : : $a = "1a"; : $a++; : :IMHO, the correct value for $a should now be "1b", not 2. Hmm... in mine as well. 'aa' goes to 'ab'; why shouldn't '1a' go to '1b'? --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."
tchrist@convex.COM (Tom Christiansen) (06/24/91)
From the keyboard of tchrist@convex.COM (Tom Christiansen): ::Exhibit 2: :: :: $a = "1a"; :: $a++; :: ::IMHO, the correct value for $a should now be "1b", not 2. : :Hmm... in mine as well. 'aa' goes to 'ab'; why shouldn't :'1a' go to '1b'? Silly me -- as I was gently reminded in mail, the regexp for string magic is documented in the man page to be: /^[a-zA-Z]*[0-9]*$/ whereas I somehow was thinking it was: /^[a-zA-Z0-9]+$/ I knew there was a reason I always wrote symbol generators as: $gensym = "symbol000000"; sub gensym { $gensym++; } $handle = &gensym; --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."
lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (06/25/91)
In article <1991Jun23.200134.18835@pronto.mh.nl> Johan Vromans <jv@mh.nl> writes:
: Perl 4.010, System Ultrix/RISC, GCC compiled.
:
: Exhibit 1:
:
: $a = "1a";
: $b = $a + 1;
:
: I would have expected an error (or at least a warning).
Would you have expected an error for this?
$a = "1\n";
$b = $a + 1;
There are many times when you want it to ignore the rest of the string just
like atof() does. Oddly enough, Perl calls atof(). How convenient. :-)
If you want to make sure your number is a number all the way to the end,
/^\d+$/ works just fine. I'm reticent to force that overhead on everyone,
however.
: Exhibit 2:
:
: $a = "1a";
: $a++;
:
: IMHO, the correct value for $a should now be "1b", not 2.
Okay, tell me the correct value for this:
$a = "1e0";
$a++;
Hint: it ain't "1e1". It may be that limiting magical increment to
values matching /^[A-za-z]*[0-9]*$/ is overkill, but you can usually
get what you want by concatenation. I suppose I could relax it to
allow /^[A-Za-z]\w*/ as well. I doubt I'll ever allow magical increment
on anything starting with digits unless it's all digits.
Larry
jv@mh.nl (Johan Vromans) (06/25/91)
(Tom Christiansen) writes: | (Johan Vromans) writes: | :: | :: $a = "1a"; | :: $a++; | :: | ::IMHO, the correct value for $a should now be "1b", not 2. | | Silly me -- as I was gently reminded in mail, the regexp for | string magic is documented in the man page to be: | | /^[a-zA-Z]*[0-9]*$/ I know. That's why I wrote 'IMHO'. (Larry Wall) writes: | Would you have expected an error for this? | | $a = "1\n"; | $b = $a + 1; Why not? (See below.) | There are many times when you want it to ignore the rest of the string just | like atof() does. Oddly enough, Perl calls atof(). How convenient. :-) This is debatable. Since perl's nature is forgiving, I can live with its current behaviour. However, if the '-w' option is used, I'd like to have a warning issued. | Okay, tell me the correct value for this: | | $a = "1e0"; | $a++; If $a is a valid number, use aritmetic. Otherwise, treat it as a string and use magical increment. You might limit validity to /\w+/. In all cases, issue a warning when '-w' is used and the syntax of the argument is not OK. Johan -- Johan Vromans jv@mh.nl via internet backbones Multihouse Automatisering bv uucp: ..!{uunet,hp4nl}!mh.nl!jv Doesburgweg 7, 2803 PL Gouda, The Netherlands phone/fax: +31 1820 62911/62500 ------------------------ "Arms are made for hugging" -------------------------
hansm@cs.kun.nl (Hans Mulder) (06/26/91)
In <1991Jun25.134732.1668@pronto.mh.nl> jv@mh.nl (Johan Vromans) writes: >(Tom Christiansen) writes: >| Okay, tell me the correct value for this: >| >| $a = "1e0"; >| $a++; >If $a is a valid number, use aritmetic. Otherwise, treat it as a string >and use magical increment. Are you sure you want this? Consider: $a="1d8"; print ++$a; # prints 1d9 print ++$a; # prints 1e0 print ++$a; # prints 2, since $a is now numeric. Magical increment, indeed. -- $level="novice"; ++$level; ++$level; ++$level; print "Just another Perl $level," Hans Mulder hansm@cs.kun.nl
jv@mh.nl (Johan Vromans) (06/27/91)
In article <3708@wn1.sci.kun.nl> hansm@cs.kun.nl (Hans Mulder) writes: > Are you sure you want this? Consider: > $a="1d8"; > print ++$a; # prints 1d9 > print ++$a; # prints 1e0 > print ++$a; # prints 2, since $a is now numeric. > > Magical increment, indeed. Okay, let's drop the magical increment part of my original posting. I still would like opinions on getting a warning (when using '-w') in situations where aritmetic is performed on a ill-formed number. In general, this would require an explicit syntax check before calling atof on a string. Johan -- Johan Vromans jv@mh.nl via internet backbones Multihouse Automatisering bv uucp: ..!{uunet,hp4nl}!mh.nl!jv Doesburgweg 7, 2803 PL Gouda, The Netherlands phone/fax: +31 1820 62911/62500 ------------------------ "Arms are made for hugging" -------------------------
flee@cs.psu.edu (Felix Lee) (06/28/91)
>In general, this would require an explicit syntax check before >calling atof on a string. You could use strtod() instead when converting numbers. Low overhead checking for properly formed numbers. -- Felix Lee flee@cs.psu.edu