in reply to How to loop over groups
Anyone developed a clean solution for this? Any advice would be appreciated.
If you use some SQL server, use its capabilities to do the work for you. I know MySQL can use:
If you're not using SQL, or your SQL doesn't have this functionality, I'd sum first, and iterate later:SELECT foo, SUM(bar) FROM xyzzy GROUP BY foo
If order is not important, leave out all @order stuff, and iterate over keys %totals;my @data = ( [ 'a', 1 ], [ 'a', 2 ], [ 'b', 3 ], [ 'b', 4 ] ); my %totals; my @order; for (@data) { push @order, $_->[0] unless exists $totals{$_->[0]}; $totals{$_->[0]} += $_->[1]; } for (@order) { # Do something with $totals{$_} }
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to loop over groups
by eisenstein (Acolyte) on Apr 02, 2002 at 08:19 UTC | |
by RMGir (Prior) on Apr 02, 2002 at 13:06 UTC |