Formatted printing

Discussion in 'Perl Misc' started by Perl Learner, Jun 14, 2005.

  1. Perl Learner

    Perl Learner Guest

    I am having trouble printing a table of values.

    The problem is that the table may contain anything from 2 character
    strings
    to signed numbers with 6 digits after the decimal.

    i am unable to align them properly.

    i tried using the following

    printf "%3.9s", $variable
    printf "%3.9f", $variable


    the later serves the purpose for floating point numbers. the former
    does somewhat what i want it to do .. it includes spaces before to fill
    it up.

    i didnt know what i was doing so i kept guessing the syntax and came up
    with the above.

    what i would like it to do is

    if this is the max length (shown in X) ..
    XXXXXXXXX
    OB
    -123.4
    123.23453
    1.2345235
    0.1231355
    0


    see how the above is neatly aligned? .. it would be great if there is a
    way i could align it like that.

    in the example above, i showed "OB" centered and the numbers aligned to
    the right.. but i dont mind if all are centered or all are aligned to
    the right as long as they're all under those "XXXXXXXXX"s

    is there a way to do that with these %s and printfs ?

    thanks
     
    Perl Learner, Jun 14, 2005
    #1
    1. Advertising

  2. Perl Learner

    Perl Learner Guest

    nevermind ...

    printf "%8.8s", $variable

    did the job.

    however, if you got better solutions, let me know.

    thanks
     
    Perl Learner, Jun 14, 2005
    #2
    1. Advertising

  3. Perl Learner

    Jim Keenan Guest

    Perl Learner wrote:
    > I am having trouble printing a table of values.
    >
    > The problem is that the table may contain anything from 2 character
    > strings
    > to signed numbers with 6 digits after the decimal.
    >
    > i am unable to align them properly.
    >
    > i tried using the following
    >
    > printf "%3.9s", $variable


    Not documented, hence outcome is unpredictable. Have you studied ...

    perldoc -f printf
    perldoc -f sprintf

    > printf "%3.9f", $variable
    >
    >
    > the later serves the purpose for floating point numbers. the former
    > does somewhat what i want it to do .. it includes spaces before to fill
    > it up.
    >
    > i didnt know what i was doing so i kept guessing the syntax and came up
    > with the above.
    >
    > what i would like it to do is
    >
    > if this is the max length (shown in X) ..
    > XXXXXXXXX
    > OB
    > -123.4
    > 123.23453
    > 1.2345235
    > 0.1231355
    > 0
    >
    >
    > see how the above is neatly aligned? .. it would be great if there is a
    > way i could align it like that.
    >
    > in the example above, i showed "OB" centered and the numbers aligned to
    > the right.. but i dont mind if all are centered or all are aligned to
    > the right as long as they're all under those "XXXXXXXXX"s
    >


    Centering a header over flush right copy sounds more like a job for
    Perl formats than one for printfs:

    perldoc perlform

    jimk
     
    Jim Keenan, Jun 14, 2005
    #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. Stuart Woodard

    Server-side Printing With Formatted Text

    Stuart Woodard, Aug 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    315
    Stuart Woodard
    Aug 3, 2003
  2. Clint MacDonald

    Borderless formatted Printing from ASP.NET

    Clint MacDonald, May 1, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    850
  3. babo
    Replies:
    9
    Views:
    3,784
    Roedy Green
    Nov 30, 2005
  4. Thomas Philips
    Replies:
    7
    Views:
    346
    Josiah Carlson
    May 21, 2004
  5. wang
    Replies:
    15
    Views:
    473
Loading...

Share This Page