format issue

R

robertchen117

My perl use printf to print:

printf "Tasks number\tTask\tLibrary\tTargets\tBytes\n";
printf "%-6d\t%-s\t%-s\t%-5d\t%-10d\n", $task_hash{$task},$2, $1,
$task_targets{$task}, $task_bytes{$task};

Tasks number Task Library Targets Bytes
1050 cmd_tsk maint_tl 1050 7340950
1000 vcs maint_tl 1000 953032
384 discover maint_tl 384 27290
213 get_v2_build_tsk relmgmt_tl 339 36951
136 runScriptUnix relmgmt_tl 136 1438804
120 wilc_tsk maint_tl 120 96432
73 v3sync_tsk maint_tl 73 19355

The format not alignment good...

I also tried this way:
printf "Number of tasks Task Library Targets Bytes
\n";
printf "%5d %s %s %8d %10d\n", $value,$2,
$1, $task_targets{$task}, $task_bytes{$task};

Anyone has good solution for print ?
 
P

prawn

I also tried this way:
printf "Number of tasks Task Library Targets Bytes
\n";
printf "%5d %s %s %8d %10d\n", $value,$2,
$1, $task_targets{$task}, $task_bytes{$task};

Anyone has good solution for print ?

format may well help you out here.

See:

perldoc -m perlform
 
P

prawn

I also tried this way:
printf "Number of tasks Task Library Targets Bytes
\n";
printf "%5d %s %s %8d %10d\n", $value,$2,
$1, $task_targets{$task}, $task_bytes{$task};

Anyone has good solution for print ?

format may well help you out here.

See:

perldoc -m perlform
 
A

anno4000

My perl use printf to print:

printf "Tasks number\tTask\tLibrary\tTargets\tBytes\n";
printf "%-6d\t%-s\t%-s\t%-5d\t%-10d\n", $task_hash{$task},$2, $1,
$task_targets{$task}, $task_bytes{$task};

Tasks number Task Library Targets Bytes
1050 cmd_tsk maint_tl 1050 7340950
1000 vcs maint_tl 1000 953032
384 discover maint_tl 384 27290
213 get_v2_build_tsk relmgmt_tl 339 36951
136 runScriptUnix relmgmt_tl 136 1438804
120 wilc_tsk maint_tl 120 96432
73 v3sync_tsk maint_tl 73 19355

The format not alignment good...

I also tried this way:
printf "Number of tasks Task Library Targets Bytes
\n";
printf "%5d %s %s %8d %10d\n", $value,$2,
$1, $task_targets{$task}, $task_bytes{$task};

Anyone has good solution for print ?

That's a job for Text::Table, available from CPAN.

use Text::Table;

my $tb = Text::Table->new(
'Tasks number', qw( Task Library Targets Bytes),
);
$tb->add( split) while <DATA>;
print $tb;

__DATA__
1050 cmd_tsk maint_tl 1050 7340950
1000 vcs maint_tl 1000 953032
384 discover maint_tl 384 27290
213 get_v2_build_tsk relmgmt_tl 339 36951
136 runScriptUnix relmgmt_tl 136 1438804
120 wilc_tsk maint_tl 120 96432
73 v3sync_tsk maint_tl 73 19355

Anno
 
G

gf

My perl use printf to print:

printf "Tasks number\tTask\tLibrary\tTargets\tBytes\n";
printf "%-6d\t%-s\t%-s\t%-5d\t%-10d\n", $task_hash{$task},$2, $1,
$task_targets{$task}, $task_bytes{$task};

Tasks number Task Library Targets Bytes
1050 cmd_tsk maint_tl 1050 7340950
1000 vcs maint_tl 1000 953032
384 discover maint_tl 384 27290
213 get_v2_build_tsk relmgmt_tl 339 36951
136 runScriptUnix relmgmt_tl 136 1438804
120 wilc_tsk maint_tl 120 96432
73 v3sync_tsk maint_tl 73 19355

The format not alignment good...

I also tried this way:
printf "Number of tasks Task Library Targets Bytes
\n";
printf "%5d %s %s %8d %10d\n", $value,$2,
$1, $task_targets{$task}, $task_bytes{$task};

Anyone has good solution for print ?


Printf is very capable of giving you the results you want. The problem
is you aren't telling printf how to do it.

As is, your format statement is telling printf() what types of fields
it's outputting, but you're not defining the widths of ALL the fields.
Instead, you're telling it to output the full width of the string
variables, then add a tab, then output the full width of the string
variable, then another tab... but strings are varying lengths so your
columns are wandering.

What you should be doing is adding in the width to each of the string
format markers.

If you don't know what those are, you can preflight your data and find
the length of the longest strings for each of the two columns, then
supply those to printf as part of the format.

And, you can do that two different ways:

1. Build the format statement dynamically after figuring out the
lengths.
2. Supply the lengths using the '*' length marker in the statements,
with the actual precomputed lengths for the fields supplied as
parameters.

Or, if your string fields will ALWAYS be within a certain length, or
you intend to enforce that length, you can just put in the lengths in
the % markers and let printf add space as needed to fill to the end of
the field. Then you can use a single space instead of the tab, to mark
the gaps in the columns.

Printf() is a really powerful tool, but, just like so many other tools
in the Perl toolbox, you have to tell it what you want it to do or
your results will be different than what you want. It can't read your
mind so you have to be very explicit when giving it directions.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top