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. Mitchua
    Replies:
    5
    Views:
    3,124
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    7,273
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. ben
    Replies:
    4
    Views:
    874
    Martin Ambuhl
    Jun 26, 2004
  4. Seong-Kook Shin

    string length limitation of printf

    Seong-Kook Shin, Jan 28, 2005, in forum: C Programming
    Replies:
    3
    Views:
    7,046
  5. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,683
  6. Spoon

    printf length modifier

    Spoon, Apr 26, 2007, in forum: C Programming
    Replies:
    12
    Views:
    1,174
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Apr 27, 2007
  7. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    628
  8. guru
    Replies:
    8
    Views:
    519
Loading...