alpha numbers

23 views
Skip to first unread message

Johan Vromans

unread,
Jun 23, 1991, 4:01:34 PM6/23/91
to
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 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" -------------------------

Tom Christiansen

unread,
Jun 24, 1991, 4:00:38 AM6/24/91
to
From the keyboard of Johan Vromans <j...@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 tch...@convex.com convex!tchrist
"So much mail, so little time."

Tom Christiansen

unread,
Jun 24, 1991, 9:58:41 AM6/24/91
to
From the keyboard of tch...@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;

Larry Wall

unread,
Jun 24, 1991, 7:16:28 PM6/24/91
to
In article <1991Jun23.2...@pronto.mh.nl> Johan Vromans <j...@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

Johan Vromans

unread,
Jun 25, 1991, 9:47:32 AM6/25/91
to

(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.

Hans Mulder

unread,
Jun 26, 1991, 10:20:37 AM6/26/91
to
In <1991Jun25....@pronto.mh.nl> j...@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 ha...@cs.kun.nl

Johan Vromans

unread,
Jun 27, 1991, 4:23:09 AM6/27/91
to

In article <37...@wn1.sci.kun.nl> ha...@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.

Felix Lee

unread,
Jun 27, 1991, 2:50:08 PM6/27/91
to
>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

Reply all
Reply to author
Forward
0 new messages