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 j...@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" -------------------------
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 tch...@convex.com convex!tchrist
"So much mail, so little time."
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;
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
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.
>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 ha...@cs.kun.nl
> 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.
You could use strtod() instead when converting numbers. Low overhead
checking for properly formed numbers.
--
Felix Lee fl...@cs.psu.edu