printf question

Discussion in 'Perl Misc' started by John Black, May 6, 2013.

  1. John Black

    John Black Guest

    Hopefully a simple question...

    Given this printf that specifies some fixed length left justified columns:

    printf "%-30s, %-30s, %-30s\n", $a, $b, $c;

    How can I make the hardcoded number 30 use a variable, say $len instead?

    This does not work:

    printf "%-$lens, %-$lens, %-lens\n", $a, $b, $c;

    I'm guessing because perl would have trouble knowing that the s at the end is not part of the
    variable? Thanks.

    John Black
     
    John Black, May 6, 2013
    #1
    1. Advertising

  2. On 2013-05-06 19:51, John Black <> wrote:
    > Hopefully a simple question...
    >
    > Given this printf that specifies some fixed length left justified columns:
    >
    > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
    >
    > How can I make the hardcoded number 30 use a variable, say $len instead?
    >
    > This does not work:
    >
    > printf "%-$lens, %-$lens, %-lens\n", $a, $b, $c;
    >
    > I'm guessing because perl would have trouble knowing that the s at the
    > end is not part of the variable?


    Yes.

    Either:

    printf "%-${len}s, %-${len}s, %-${len}s\n", $a, $b, $c;

    Or (more C-like):

    printf "%-*s, %-*s, %-*s\n", $len, $a, $len, $b, $len, $c;

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaƟt. -- Ralph Babel
     
    Peter J. Holzer, May 6, 2013
    #2
    1. Advertising

  3. John Black

    hymie! Guest

    In our last episode, the evil Dr. Lacto had captured our hero,
    John Black <>, who said:
    >Hopefully a simple question...
    >
    >Given this printf that specifies some fixed length left justified columns:
    >
    > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
    >
    >How can I make the hardcoded number 30 use a variable, say $len instead?


    $format = "%-${len}s, %-${len}s, %-${len}s\n";
    printf $format, $a, $b, $c;

    --hymie! http://lactose.homelinux.net/~hymie
    -------------------------------------------------------------------------------
     
    hymie!, May 6, 2013
    #3
  4. John Black

    John Black Guest

    In article <>, says...
    >
    > On 2013-05-06 19:51, John Black <> wrote:
    > > Hopefully a simple question...
    > >
    > > Given this printf that specifies some fixed length left justified columns:
    > >
    > > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
    > >
    > > How can I make the hardcoded number 30 use a variable, say $len instead?
    > >
    > > This does not work:
    > >
    > > printf "%-$lens, %-$lens, %-lens\n", $a, $b, $c;
    > >
    > > I'm guessing because perl would have trouble knowing that the s at the
    > > end is not part of the variable?

    >
    > Yes.
    >
    > Either:
    >
    > printf "%-${len}s, %-${len}s, %-${len}s\n", $a, $b, $c;
    >
    > Or (more C-like):
    >
    > printf "%-*s, %-*s, %-*s\n", $len, $a, $len, $b, $len, $c;
    >
    > hp


    Thanks much. I guess with Perl, its probably going to be adding either {}, [] or ().

    John Black
     
    John Black, May 6, 2013
    #4
  5. John Black

    John Black Guest

    In article <51880e40$0$36530$>, says...
    >
    > In our last episode, the evil Dr. Lacto had captured our hero,
    > John Black <>, who said:
    > >Hopefully a simple question...
    > >
    > >Given this printf that specifies some fixed length left justified columns:
    > >
    > > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
    > >
    > >How can I make the hardcoded number 30 use a variable, say $len instead?

    >
    > $format = "%-${len}s, %-${len}s, %-${len}s\n";
    > printf $format, $a, $b, $c;


    I like the idea of putting $format in its own variable for readability and for use in
    multiple printf statements. Thanks.

    John Black
     
    John Black, May 6, 2013
    #5
    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. Edith Gross
    Replies:
    2
    Views:
    347
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    643
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,300
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    452
  5. guru
    Replies:
    8
    Views:
    293
Loading...

Share This Page