Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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


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.


Report this thread to moderator Post Follow-up to this message
Old Post
J Krugman
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
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

Report this thread to moderator Post Follow-up to this message
Old Post
YYUsenet
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
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);

Report this thread to moderator Post Follow-up to this message
Old Post
Tassilo v. Parseval
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
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

Report this thread to moderator Post Follow-up to this message
Old Post
xhoster@gmail.com
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
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


Report this thread to moderator Post Follow-up to this message
Old Post
John Bokma
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
In <slrnd6r19k.23f.tassilo.von.parseval@localhost.localdomain> "Tassilo v. P
arseval" <tassilo.von.parseval@rwth-aachen.de> writes:

>Also sprach J Krugman:
 

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


Report this thread to moderator Post Follow-up to this message
Old Post
J Krugman
04-26-05 08:59 AM


Re: '2 apples' + '2 oranges' == 4
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);

Report this thread to moderator Post Follow-up to this message
Old Post
Tassilo v. Parseval
04-26-05 01:58 PM


Re: '2 apples' + '2 oranges' == 4
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


Report this thread to moderator Post Follow-up to this message
Old Post
John Bokma
04-26-05 01:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:28 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.