printf and variable length?

Discussion in 'Perl Misc' started by Daemonice, Oct 25, 2005.

  1. Daemonice

    Daemonice Guest

    Hello,

    I am using the below statement. It writes exactly 19 positions,
    padding the right of the string with spaces if need be.

    printf DB "%-19s",$value;

    However, now I need to write $fieldlength positions. I cannot find how
    I can combine printf with $fieldlength. Is this possible, or should I
    first format my string and then print it? What is the most ecomical
    way to do this?

    TIA,
    Daemonice.
     
    Daemonice, Oct 25, 2005
    #1
    1. Advertisements

  2. Daemonice <> wrote:

    > printf DB "%-19s",$value;
    >
    > However, now I need to write $fieldlength positions. I cannot find how
    > I can combine printf with $fieldlength.



    You can disambiguate the name of the variable with curly braces
    (so that the "s" is not part of the variable name):

    printf DB "%-${fieldlength}s", $value;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 25, 2005
    #2
    1. Advertisements

  3. Daemonice

    Daemonice Guest

    Thanks to everybody who answered, it was very useful for me!
     
    Daemonice, Oct 27, 2005
    #3
    1. Advertisements

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:
    697
    Martin Ambuhl
    Jun 26, 2004
  2. Seong-Kook Shin

    string length limitation of printf

    Seong-Kook Shin, Jan 28, 2005, in forum: C Programming
    Replies:
    3
    Views:
    6,833
  3. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

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

Share This Page