'2 apples' + '2 oranges' == 4

J

J Krugman

I can't find the documentation for the behavior illustrated in the
Subject line (i.e. in Perl arithmetic operations are defined for
strings beginning with numbers[*]). Any pointers would be appreciated.
(Also, meta-pointers on how one would go about finding this
documentation if one didn't know already would also be very welcome.)

TIA!

jill

* Actually, the format of the strings Perl will perform arithmetic
on is more compilicated than I described above. Optional leading
whitespace is permitted, for example.
 
T

Tassilo v. Parseval

Also sprach J Krugman:
I can't find the documentation for the behavior illustrated in the
Subject line (i.e. in Perl arithmetic operations are defined for
strings beginning with numbers[*]). Any pointers would be appreciated.
(Also, meta-pointers on how one would go about finding this
documentation if one didn't know already would also be very welcome.)

A string like '2 apples' and '2 oranges' in numeric context will both
become 2. So if you add them, they are both evaluated in numeric
context, so that becomes 2 + 2, and hence 4.

The rules for string to number conversion are not so hard. One simple
rule is that perl scans everything up to the first non-digit character
and throws everything beginning with this character away. So the
number-prefix remains and becomes the number.

Tassilo
 
X

xhoster

J Krugman said:
I can't find the documentation for the behavior illustrated in the
Subject line (i.e. in Perl arithmetic operations are defined for
strings beginning with numbers[*]). Any pointers would be appreciated.

The warnings you get if you enable warnings would be a good start.

Or perldoc perlintro:

Scalars
A scalar represents a single value:

my $animal = "camel";
my $answer = 42;

Scalar values can be strings, integers or floating point
numbers, and Perl will automatically convert between them as
required.

Xho
 
J

John Bokma

Tassilo said:
The rules for string to number conversion are not so hard. One simple
rule is that perl scans everything up to the first non-digit character
and throws everything beginning with this character away. So the
number-prefix remains and becomes the number.

perl -e "print '3e4' * 4"
 
J

J Krugman

In said:
Also sprach J Krugman:
I can't find the documentation for the behavior illustrated in the
Subject line (i.e. in Perl arithmetic operations are defined for
strings beginning with numbers[*]). Any pointers would be appreciated.
(Also, meta-pointers on how one would go about finding this
documentation if one didn't know already would also be very welcome.)
A string like '2 apples' and '2 oranges' in numeric context will both
become 2. So if you add them, they are both evaluated in numeric
context, so that becomes 2 + 2, and hence 4.
The rules for string to number conversion are not so hard. One simple
rule is that perl scans everything up to the first non-digit character
and throws everything beginning with this character away. So the
number-prefix remains and becomes the number.

I'm sure the rules are straightforward, but I don't understand why
they aren't documented. It is not wise to use undocumented features
even if they are extremely straightforward.

jill
 
J

John Bokma

Tassilo said:
Also sprach John Bokma:


Which is why I wrote "One simple rule". It implies there are others.

I think the rule is: scans everything that looks like a number, including
scientific notation, sign etc., and stops when it can't add anything more
to this number.

Correct me if I am wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top