Perl Monk, Perl Meditation | |
PerlMonks |
Ways to group elements of an AoHby bradcathey (Prior) |
on Jan 04, 2009 at 02:30 UTC ( [id://733972]=perlquestion: print w/replies, xml ) | Need Help?? |
bradcathey has asked for the wisdom of the Perl Monks concerning the following question: Fellow Monasterians, As a web developer I spend lots of time with HTML::Template and it's requirement for AoH in it's <tmpl_loop ...> function, which I use frequently. Many of the lists I loop in the browser need to be grouped first. It's great when I can use GROUP BY when querying the database, but often I don't have that luxury. My question is, what's the best way to group a list? My example has several entries for a few users that I want to group, including sums of hours worked and a monetary extension.
My first attempt sorts first and then iterates over the list—not especially efficient and a little goofy with the -1 counter:
I reworked it to eliminate the sort and cut the processing time basically in half:
OUTPUT for both methods are the same:
But even the second way seems a bit tedious with that second while loop to build the final array. Is there a better way?
—Brad "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Back to
Seekers of Perl Wisdom
|
|