Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl Monk, Perl Meditation
 
PerlMonks  

Re: (Complex)Data Manipulation

by TedPride (Priest)
on Feb 28, 2005 at 02:52 UTC ( [id://434992]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to (Complex)Data Manipulation

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://434992]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.