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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



dissectcode
Novice

Oct 1, 2010, 11:09 AM

Post #1 of 2 (303 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:


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


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


shawnhcorey
Enthusiast


Oct 1, 2010, 1:48 PM

Post #2 of 2 (301 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.

__END__

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

Perl documentation is available at perldoc.perl.org. 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