printf and print

Discussion in 'Perl Misc' started by perler wannabe, Nov 3, 2010.

  1. Hi,

    Here is my script:

    #!/usr/bin/perl

    chomp(my @string = <STDIN>);

    print "1234567890" x 6,"\n";
    foreach (@string) {
    printf "%20s\n";
    }

    This gives me the output I want which is: my strings reight-justified,
    20 characters column as the following:

    1234567890123456789012345678901234567890123456789012345678901234567890
    perL
    c++
    javascript

    but if I use printf instead of print in my script as the following:
    printf "1234567890" x 6,"\n";

    it will give me something like the following:
    1234567890123456789012345678901234567890123456789012345678901234567890
    perL
    c++
    javascript

    what makes this distinct behavior?

    Thanks
    perler wannabe, Nov 3, 2010
    #1
    1. Advertising

  2. On Nov 3, 11:17 pm, Tad McClellan <> wrote:
    > perler wannabe <> wrote:
    > > Here is my script:

    >
    > If you say so...
    >
    > > #!/usr/bin/perl

    >
    > You should ask for all the help you can get by enabling warnings
    > and strictures in every Perl program:
    >
    >     use warnings;
    >     use strict;
    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    > > chomp(my @string = <STDIN>);

    >
    > > print "1234567890" x 6,"\n";
    > > foreach (@string) {
    > > printf "%20s\n";

    >
    >           ^^^^
    >           ^^^^ what does that get replaced with?
    >
    > > }

    >
    > > This gives me the output I want

    >
    > No it doesn't.
    >
    > You should copy/paste code, not (attempt to) retype it.
    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    > > which is: my strings reight-justified,
    > > 20 characters column as the following:

    >
    > > 1234567890123456789012345678901234567890123456789012345678901234567890
    > >                                     perL
    > >                                      c++
    > >                             javascript

    >
    > That is not the output from the code you posted.
    >
    > That is not right justified.
    >
    > That does not line up at 20 characters.
    >
    > Have we entered the Twilight Zone?
    >
    > > but if I use printf instead of print in my script as the following:
    > > printf "1234567890" x 6,"\n";

    >
    > > it will give me something like the following:
    > > 1234567890123456789012345678901234567890123456789012345678901234567890
    > > perL
    > >                                      c++
    > >                             javascript

    >
    > > what makes this distinct behavior?

    >
    > printf's format string does not contain any format specifiers, so all
    > of the rest of the arguments to printf (ie. "\n") are ignored.
    >
    >     printf "1234567890" x 6 . "\n";
    >
    > but using printf() without any format specifiers is an abomination,
    > so don't do that in Real Code.
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
    > The above message is a Usenet post.
    > I don't recall having given anyone permission to use it on a Web site.


    Hi Tad, thanks for your reply. Indeed I have to rewrite my script and
    the output to post it here. It's quite complicated, but in short, I
    script in a pc having no internet connection, for now. I'll try to
    post better now on :).

    %20s inside of the double quote of my printf should bed replaced by $_
    of course.

    So my script should look like the following:

    #!/usr/bin/perl


    chomp(my @string = <STDIN>);


    print "1234567890" x 6,"\n"; #ruler
    foreach (@string) {
    printf "%20s\n", $_;

    }

    forget about the retyped outputs... The output of the above script
    gives me 20 chars right-justified. And with printf, the first string
    doesn't
    perler wannabe, Nov 3, 2010
    #2
    1. Advertising

  3. On Wed, 03 Nov 2010 10:10:51 -0700, perler wannabe wrote:

    > forget about the retyped outputs... The output of the above script gives
    > me 20 chars right-justified. And with printf, the first string doesn't


    It does, but the "\n" is ignored in the printf. Please carefully reread
    the printf documentation.

    M4
    Martijn Lievaart, Nov 3, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    596
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,212
  3. ern
    Replies:
    6
    Views:
    347
    Clark S. Cox III
    Jan 18, 2006
  4. keto
    Replies:
    0
    Views:
    881
  5. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    418
Loading...

Share This Page