Very neat, though it assumes a rather precise format. My solution is more adaptive, if a good deal more messy:
@mon = ('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct' +,'Nov','Dec'); $_ = <DATA>; chomp; $n{$_} = $c++ for split /\t/; for (<DATA>) { chomp; split /\t/; @date = split /\//, $_[$n{'Date'}]; $product = $_[$n{'Product'}]; $country = $_[$n{'Imported_From'}]; $quantity = $_[$n{'Quantity'}]; $countries{$country} = () if !exists $countries{$country}; $totals{$date[2]}{$date[0]}{$product}{$country} += $quantity; } @countries = sort keys %countries; for (sort {$a <=> $b} keys %totals) { $year = $totals{$_}; for (sort {$a <=> $b} keys %$year) { $month = $year->{$_}; print "$mon[$_]\n"; print join("\t", 'Product', @countries, 'Total'), "\n"; for (sort keys %$month) { print; $product = $month->{$_}; $total = 0; for (@countries) { print "\t".$product->{$_}; $total += $product->{$_}; } print "\t$total\n"; } print "\n"; } } __DATA__ Date Product Quantity Imported_From 1/2/04 Shirts 32 Australia 1/9/04 Shoes 234 Asia 2/12/04 Caps 109 UK 4/4/04 Shoes 6 Asia 4/4/04 Shirts 12 Australia 5/6/04 Shirts 398 Australia
You can have any number of fields in any order, and the records can also be out of order. You're not limited to three countries either. Results are given in tab delimited format, perhaps not the most pretty thing to use, but I didn't feel like messing with prints after spending so much time writing the algorithm.

In reply to Re: (Complex)Data Manipulation by TedPride
in thread (Complex)Data Manipulation by rupesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.