Formatted printing

P

Perl Learner

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
 
P

Perl Learner

nevermind ...

printf "%8.8s", $variable

did the job.

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

thanks
 
J

Jim Keenan

Perl said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top