CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Why is "2*4" - 0 equal to 2??



Oct 1, 2010, 11:09 AM

Post #1 of 2 (1096 views)
Why is "2*4" - 0 equal to 2?? Can't Post

I was reading the "learning perl" oreilly book and it says that the string "2*4" used as a number is actually the number 2. I can force it to be treated as a number by doing this:

print "2*4" - 0, "\n";

Can someone tell me why it is 2? thanks a lot


Oct 1, 2010, 1:48 PM

Post #2 of 2 (1094 views)
Re: [dissectcode] Why is "2*4" - 0 equal to 2?? [In reply to] Can't Post

Because it's a string. When converting string to numbers, Perl looks for a number but stops at the first character that cannot part of a number, in this case, the asterisk. So, Perl thinks the number for "2*4" is two.


I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives