Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone, I need a help with a slight report script format. I have this as an output of an query:-
rep1 product1 2 rep1 product2 2 rep2 product3 3 rep3 product4 4 rep3 product5 5
I need this to look like this:-
rep1 rep2 rep3 product1 2 product2 2 product3 3 product4 4 product5 5
The important point to note is that the reps might repeat but the products will not repeat for the reps, in the sense that each rep has his own productline. Thank you

20040713 Edit by ysth: added code tags to preserve formatting

20040722 Edit by castaway: Changed title from 'Reporting doubt'

Replies are listed 'Best First'.
Re: Report Formatting Help
by davidj (Priest) on Jul 14, 2004 at 03:02 UTC
    Ok, this is most definately a quick hack, but it does work. I tweaked with the formatting of the output to match the structure of what you are looking for given the format of the data you supplied. Obviously, you will need to figure out the formatting for different data. (note the extra @reps data structure is required for output formatting purposes).

    #!/usr/bin/perl use strict; my ($p, $r, $v); my (%data, %products, %reps, @reps); #iterate over data and store in hash while(<DATA>) { chomp($_); ($r, $p, $v) = split(" ", $_); $data{$p}->{$r} = $v; $products{$p} = 1; $reps{$r} = 1; @reps = sort keys %reps; } # start formatting output print " " x 10; foreach (sort keys %reps) { print "$_", " " x 4; } print "\n"; #iterate over products foreach (sort keys %products) { print "$_"; # iterate over products for( my $i = 0; $i < scalar @reps; $i++) { # see if rep has that product and print if it does if( exists($data{$_}->{$reps[$i]}) ) { print " " x 3, " " x ($i * 8), " $data{$_}->{$reps[$i]}"; } } print "\n"; } exit; __DATA__ rep1 product1 2 rep1 product2 2 rep2 product3 3 rep3 product4 4 rep3 product5 5
    output:
    155.~/perl/tmp > ./am.pl rep1 rep2 rep3 product1 2 product2 2 product3 3 product4 4 product5 5
    hope this helps,

    davidj
      Hello again, Thank you very much for your help...it was a very good suggestion......It helped me submit a report just in time...
Re: Report Formatting Help
by TilRMan (Friar) on Jul 14, 2004 at 03:01 UTC
    #!/usr/bin/perl -w use strict; use Text::Table; my %data; while (<>) { my ( $rep, $product, $foo ) = split ' '; $data{$rep}{$product} = $foo; } my @reps = sort keys %data; my $table = Text::Table->new( '', @reps ); for my $i ( 0 .. $#reps ) { my $rep = $reps[$i]; my @products = sort keys %{ $data{$rep} }; foreach (@products) { $table->add( $_, ('') x $i, $data{$rep}{$_} ); } } print $table;

    BTW, having never used Text::Table before, I found it surprisingly powerful. Most of that power is unused on this program.

Re: Report Formatting Help
by rjbs (Pilgrim) on Jul 14, 2004 at 02:36 UTC
    First you want to gather the information into a structure suitable for output. You want to output, for every product, quantity by rep. So, let's store it that way:
    my %products; for (@lines) { chomp; next unless my ($rep, $prod, $qty) = $_ =~ /(\w+)\s+(\w+)\s+(\d+)/; $product{$prod}{$rep} += $qty; }
    Now you have a two-level hash of the data. At this point, I would suggest using Text::Table to produce the output. Failing that, it's really just: foreach product, foreach column, "print spaced out columns"
    rjbs
      hello again, Thank you for the help...but I had this part figured...I could put the code till the hash myself...I need help with how to represent the hash.
Re: Report Formatting Help
by rjbs (Pilgrim) on Jul 14, 2004 at 01:59 UTC
    I think the simplest way to get the output you want would be with this code:
    print "rep1 rep2 rep3 product1 2 product2 2 product3 3 product4 4 prod +uct5 5";
    Of course, this might not be useful for all input. Maybe if you gave us some clearer input parameters, possibly formatted with pre, and some idea of the rules you want to apply, we could suggest soemthing more generalized.
    rjbs
      Sorry if I made this reply look foolish by editing the root node. It was laid out in a table format, but without tags to preserve the formatting, so it looked like what rjbs shows here.

      Just a reminder to everyone; you can see how a node was entered by looking at the source of the xml view (for which there is a link just under the node title under the search field at the top of the page).