in reply to Grouping Objects by Attribute
I'd do something like:
my @monks = ...; # Array of Monk objects, with a level method. my %monks_by_level; push(@{$monks_by_level {$_->level()}}, $_) for @monks; while (my ($level, $monks) = each(%monks_by_level)) { callToPrayer($monks) }
If you settle for passing in a list (and hence, losing any reference (no pun intended) to an array), you can do it "without references" (well, the objects you start out with are references) - if you know which levels there are (but you could find out by making another pass):
The first code fragment needs only one pass over the array of objects - the last fragment needs a pass for each level.foreach my $level (1 .. 10) { callToPrayer(grep($_->level() == $level, @monks)); }
|
|---|