Formatted printing

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am having trouble printing a table of values.

The problem is that the table may contain anything from 2 character
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) ..

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 ?


Re: Formatted printing

nevermind ...

printf "%8.8s", $variable

did the job.

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


Re: Formatted printing

Perl Learner wrote:
Quoted text here. Click to load it

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

    perldoc -f printf
    perldoc -f sprintf

Quoted text here. Click to load it

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

    perldoc perlform


Site Timeline