For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > '2 apples' + '2 oranges' == 4









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author '2 apples' + '2 oranges' == 4
J Krugman

2005-04-26, 3:59 am




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.



--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

YYUsenet

2005-04-26, 3:59 am

J Krugman wrote:
> 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.
>
>
>


Please do not put your whole message in the subject line.
I have no idea what you are trying to get at.

Please be a little bit more descriptive and provide some sample code.

--
k g a b e r t (at) x m i s s i o n (dot) c o m

*Use Mozzila/Firefox*!
http://www.spreadfirefox.com/?q=user/register&r=71209
Tassilo v. Parseval

2005-04-26, 3:59 am

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
--
use bigint;
$n=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
xhoster@gmail.com

2005-04-26, 3:59 am

J Krugman <jkrugman345@yahbitoo.com> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
John Bokma

2005-04-26, 3:59 am

Tassilo v. Parseval wrote:

> 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"

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

J Krugman

2005-04-26, 3:59 am

In <slrnd6r19k.23f.tassilo.von.parseval@localhost.localdomain> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:

>Also sprach J Krugman:


[color=darkred]
>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

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

Tassilo v. Parseval

2005-04-26, 8:58 am

Also sprach John Bokma:

> Tassilo v. Parseval wrote:
>
>
> perl -e "print '3e4' * 4"


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

Tassilo
--
use bigint;
$n=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
John Bokma

2005-04-26, 8:58 am

Tassilo v. Parseval wrote:

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

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com