@mon = ('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); $_ = ; chomp; $n{$_} = $c++ for split /\t/; for () { 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