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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.