Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Print Statement

by msbalaji (Chaplain)
on Feb 17, 2006 at 06:33 UTC ( [id://530907]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

msbalaji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a doubt about print statement as,

#!/usr/bin/perl use warnings; use strict; print 25_000_000, " ", -4, "\n"; expected output 25_000_000 -4 but getting 25000000 -4

What's happening?

Thanks in advance,
Balaji M.

Replies are listed 'Best First'.
Re: Print Statement
by izut (Chaplain) on Feb 17, 2006 at 06:41 UTC
    Perl treats 25_000_000 as integer. To print it correctly, you should do something like this:
    print "25_000_000", " ", -4, "\n";

    Igor 'izut' Sutton
    your code, your rules.

Re: Print Statement
by Fletch (Bishop) on Feb 17, 2006 at 08:57 UTC

    a) You have a "question", not a "doubt";
    b) see perldoc perldata in the section "Scalar value constructors" where it covers that an underscore may be used in a numeric literal for readability (but perl will silently discard them after parsing your source).

      a) You have a "question", not a "doubt";

      The OP's original language is not English. Doubt is a reasonable translation from several languages.

        Yes, and I'm trying to help them become more idiomatic speakers of English; but I'll be sure to remember that the next time I see someone taking doubts from reporters at a press conference.

        Update: And I'm not the only person around here it annoys either (c.f. "Question" vs "Doubt").

Re: Print Statement
by ikegami (Patriarch) on Feb 17, 2006 at 10:58 UTC
    1225.5, 1225.50, 12255E-1 and 1_225.5 are all stored indentically internally, and print will print them all as 1225.5 because that is how print displays the value 1225.5. print has no idea how the number came to be; it just knows its value. If you wish to preserve the underscores, you want to treat the number as a string instead of as a number, so put quotes around it.
      I was trying to do :
      perl -e 'print 0100101."\n"'
      As expected I got the decimal equivalent of octal as:
      32833
      However, doing
      perl -e 'print 100101."\n"'
      returned
      String found where operator expected at -e line 1, near "100101."\n"" (Missing operator before "\n"?) syntax error at -e line 1, near "100101."\n"" Execution of -e aborted due to compilation errors.
      Any idea why this fuss about a zero? Not a burning problem or something. Just curious.

        The "." is part of the number.

        print 100101. "\n" ^ missing operator

        Add a space before the ".".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://530907]
Approved by Samy_rio
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.