Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Order of printf statements vs order of actual lines printed out

by sg (Pilgrim)
on Apr 09, 2013 at 04:47 UTC ( #1027643=perlquestion: print w/ replies, xml ) Need Help??
sg has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

In the following code, note the order of the printf statements; in particular, note the order of the second and the third printfs:

#!/c/opt/perl64/bin/perl BEGIN {(*STDERR = *STDOUT) || die;} use diagnostics; use warnings; use strict; $| = 1; my @foo; push @foo, 5; push @foo, 7, 8; printf "@foo\n"; printf "\nabove first set of dashes #: [" . #@foo . "]\n"; printf "first set of dashes------------\n"; printf "\nsecond set of dashes------------\n"; my $y = printf "\ny dashes------------\n"; printf "\ny: [$y]\n"; __END__

The output of the above code using perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x64-multi-thread (with 1 registered patch) is given below -- note the reversal of the outputs from the second and third printf statements:

5 7 8 first set of dashes------------ above first set of dashes #: [1 second set of dashes------------ y dashes------------ y: [1] $

Question: why is the order of the output lines different from the order of the printf statements?

Thanks,

Comment on Order of printf statements vs order of actual lines printed out
Select or Download Code
Re: Order of printf statements vs order of actual lines printed out
by davido (Archbishop) on Apr 09, 2013 at 05:20 UTC

    It's because of the error right here:

    printf "\nabove first set of dashes#: [" . #@foo . "]\n"; # The error is -------------------here------^^

    So, what's happening? By typing #@ when you meant to type $# you initiated a comment. So "@foo . "\n"; was no longer code, but rather, a comment. That made the line in question look like this:

    printf "\nabove first set of dashes#: [" . printf "first set of dashes------------\n";

    Notice how you're using the concatenation operator, "." to append the output from "printf  "first set of dashes------------\n";" to the string "\nabove first set of dashes#:  [". Before that concatenation can occur, that second printf has to be evaluated to obtain its return value (which is "1"). And in so doing, its side effect (in the functional sense) is taking place: It's printing the "first set of dashes....." line.


    Dave

Re: Order of printf statements vs order of actual lines printed out
by Rahul6990 (Beadle) on Apr 09, 2013 at 06:20 UTC
    Use:

    my @foo; push @foo, 5; push @foo, 7, 8; printf "@foo\n"; printf "\nabove first set of dashes #: [$#foo]\n"; printf "first set of dashes------------\n"; printf "\nsecond set of dashes------------\n"; my $y = printf "\ny dashes------------\n"; printf "\ny: [$y]\n";
    ---------------------------------------------------------- As you are using (") double quotes the concatenation operator is not required , so we can ignore that.
Re: Order of printf statements vs order of actual lines printed out
by Jenda (Abbot) on Apr 09, 2013 at 09:52 UTC

    Do not use printf() when you mean print() !!!

    my @foo = ("This is 100 % clean.", "Hello."); printf "@foo\n";

    Try the code above and see for yourself!

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027643]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-21 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (169 votes), past polls