in reply to Re: Re: How to loop over groups
in thread How to loop over groups

Sure, just rearrange your data.

By building a hash which groups the rows together, your problem goes away.

@a = (['a',1],['a',2],['b',3],['b',4]); # "db read" my %aByGroup; foreach(@a) { # you could just push $_->[1], but I'm guessing your # data is more complex, so I'm keeping the whole row push @{$aByGroup{$_->[0]}},$_; } my $old; foreach my $group (sort keys %aByGroup) { my $total = 0; # init print "Section $group\n"; foreach my $row(@{$aByGroup{$group}}) { $total += $row->[1]; # processrow block print "$row->[1]\n"; } print "$group: $total\n"; # finish row block }

--
Mike