format issue

Discussion in 'Perl Misc' started by robertchen117@gmail.com, Mar 2, 2007.

  1. Guest

    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 ?
     
    , Mar 2, 2007
    #1
    1. Advertising

  2. prawn Guest

    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
     
    prawn, Mar 2, 2007
    #2
    1. Advertising

  3. prawn Guest

    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
     
    prawn, Mar 2, 2007
    #3
  4. -berlin.de Guest

    <> 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
     
    -berlin.de, Mar 2, 2007
    #4
  5. gf Guest

    On Mar 1, 11:31 pm, ""
    <> 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.
     
    gf, Mar 2, 2007
    #5
    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. Brian Candy
    Replies:
    2
    Views:
    1,168
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,263
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,598
  4. shannon
    Replies:
    0
    Views:
    573
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    465
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page