padding left-justified string fields

Discussion in 'Perl' started by Dave, Jul 14, 2004.

  1. Dave

    Dave Guest

    I've written a perl program to manipulate data formats so that I can import
    data from an estimating software program to an accounting program. Works
    just fine, with the following caveat: The record identifier field is
    numeric in the estimating program but it can be (and is interpreted as) a
    string in the accounting software. Hence, when I bring up project data in
    the accounting software, my items look like so:

    10
    100
    110
    ....
    190
    20
    210
    220

    These fields MUST be left-justified for importation into the account s/w, so
    I am using "@<<<<<<" in my format block. I'd like to be able to pad all
    items to four decimal places with zeroes so that I'd end up with:

    010
    020
    030
    ....
    090
    100
    etc.

    Can anyone tell me of a simple way to do this in perl? I've read and read
    and read the manpages on printf() and sprintf() but can't seem to figure out
    the syntax. Basically I'm printing out 19 fields, the first three are
    left-justified (strings) with varying lengths and the rest for the most part
    are seven-dot-three numerics. It would have been easier I'm sure to use
    printf with some modifiers rather than type in all those "###'s" and
    "<<<'s". Can anyone point me to an online resource to figure out printf
    with some real world examples?

    Thanks

    Dave
     
    Dave, Jul 14, 2004
    #1
    1. Advertising

  2. Dave

    Joe Smith Guest

    Dave wrote:

    > I'd like to be able to pad all items to 3 decimal places with zeroes
    > Can anyone tell me of a simple way to do this in perl?


    It's simple.

    printf "%03d %4d %s\n", $number1, $number2, $string;

    That will make $number1 be padded to three places with zeros
    and $number2 padded to four places with spaces, followed
    by a string and ending with a newline.
    -Joe
     
    Joe Smith, Jul 16, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    "Joe Smith" <> wrote in message
    news:t8HJc.65901$WX.45392@attbi_s51...
    > Dave wrote:
    >
    > > I'd like to be able to pad all items to 3 decimal places with zeroes
    > > Can anyone tell me of a simple way to do this in perl?

    >
    > It's simple.
    >
    > printf "%03d %4d %s\n", $number1, $number2, $string;
    >

    Thanks Joe. Can you tell me how to specify left vs. right-justified with
    printf? Also, if I have a 7-dot-3 decimal format, is it possible to specify
    the location of the decimal point?

    thx

    Dave

    > That will make $number1 be padded to three places with zeros
    > and $number2 padded to four places with spaces, followed
    > by a string and ending with a newline.
    > -Joe
    >
     
    Dave, Jul 20, 2004
    #3
  4. Dave wrote:
    > [...] Can you tell me how to specify left vs. right-justified
    > with printf? Also, if I have a 7-dot-3 decimal format, is it
    > possible to specify the location of the decimal point?


    Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?

    jue
     
    Jürgen Exner, Jul 20, 2004
    #4
  5. Dave

    Dave Guest

    "Joe Smith" <> wrote in message
    news:t8HJc.65901$WX.45392@attbi_s51...
    > Dave wrote:
    >
    > > I'd like to be able to pad all items to 3 decimal places with zeroes
    > > Can anyone tell me of a simple way to do this in perl?

    >
    > It's simple.
    >
    > printf "%03d %4d %s\n", $number1, $number2, $string;
    >
    > That will make $number1 be padded to three places with zeros
    > and $number2 padded to four places with spaces, followed
    > by a string and ending with a newline.


    Okay, that's all good. But my predicament is that I want something like
    "10" padded to "010" AND I want it left-justified... the manpage for printf
    states "If the 0 and - flags both appear, the 0 flag is ignored." Also, my
    input format requires fixed-width fields; hence if I've got "10" and I
    choose pad (i.e. printf "%010d") I end up with 0000000010 which I definitely
    don't want imported as my string.

    I guess maybe I could use printf to pad my values, then format to output
    properly re: left-justification, etc.
     
    Dave, Jul 20, 2004
    #5
  6. Dave

    Dave Guest

    "Jürgen Exner" <> wrote in message
    news:YOaLc.29375$...
    > Dave wrote:
    > > [...] Can you tell me how to specify left vs. right-justified
    > > with printf? Also, if I have a 7-dot-3 decimal format, is it
    > > possible to specify the location of the decimal point?

    >
    > Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?
    >
    > jue
    >

    I did. I swear it. I tried many combinations and permutations. I ended up
    using a two-step approach: I used printf to pad my values as part of my
    array assignment loop, then format/write for justification and field
    placement. I could use printf twice, but already had the output format all
    set up for write. This approach ended up being the simplest for me, not
    being skilled in the ways of printf.

    I can see that in the big picture, printf is much more economical (saves
    many many keystrokes) and I will probably put some time into broadening my
    understanding of it.
     
    Dave, Jul 20, 2004
    #6
  7. Dave

    Joe Smith Guest

    Dave wrote:

    > "Jürgen Exner" <> wrote in message
    > news:YOaLc.29375$...
    >
    >>Dave wrote:
    >>
    >>>[...] Can you tell me how to specify left vs. right-justified
    >>>with printf? Also, if I have a 7-dot-3 decimal format, is it
    >>>possible to specify the location of the decimal point?

    >>
    >>Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?
    >>
    >>jue
    >>

    >
    > I did. I swear it.


    You didn't read all of it.

    You've missed the line with "left-justify within the field".
    Go back and read it again.
    -Joe
     
    Joe Smith, Jul 22, 2004
    #7
  8. Dave

    Joe Smith Guest

    Dave wrote:

    > Okay, that's all good. But my predicament is that I want something like
    > "10" padded to "010" AND I want it left-justified... the manpage for printf
    > states "If the 0 and - flags both appear, the 0 flag is ignored."


    It appears you did not recognize the significance of this line:

    .number "precision": digits after decimal point for
    floating-point, max length for string, minimum length
    for integer

    That line explains what you should expect from
    printf "%10.3\n",99;

    -Joe
     
    Joe Smith, Jul 22, 2004
    #8
    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. RA
    Replies:
    1
    Views:
    384
  2. Replies:
    5
    Views:
    1,133
    Ian Rastall
    Jun 29, 2005
  3. cjl
    Replies:
    11
    Views:
    844
    Bengt Richter
    Mar 26, 2005
  4. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Left justified or center justified under XSL:FO

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Feb 9, 2007, in forum: XML
    Replies:
    0
    Views:
    509
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Feb 9, 2007
  5. Ben Thomas
    Replies:
    7
    Views:
    296
    Jan Svitok
    Nov 16, 2006
Loading...

Share This Page