Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > format issue

Reply
Thread Tools

format issue

 
 
robertchen117@gmail.com
Guest
Posts: n/a
 
      03-02-2007
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 ?

 
Reply With Quote
 
 
 
 
prawn
Guest
Posts: n/a
 
      03-02-2007
On Thu, 01 Mar 2007 22:31:51 -0800, wrote:

> 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 BotM#1 LotR#9
 
Reply With Quote
 
 
 
 
prawn
Guest
Posts: n/a
 
      03-02-2007
On Thu, 01 Mar 2007 22:31:51 -0800, wrote:

> 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 BotM#1 LotR#9
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      03-02-2007
<> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
gf
Guest
Posts: n/a
 
      03-02-2007
On Mar 1, 11:31 pm, "robertchen...@gmail.com"
<robertchen...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Money Format + Decimal Place Format shannon Java 1 02-02-2006 03:47 PM
Money Format + Decimal Place Format shannon Java 0 02-01-2006 10:02 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
NTFS quick format and normal format Guan Foo Wah MCSE 2 05-08-2004 11:35 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57