in reply to Array Folding

Re-reading OP again, i think i understood this time ... I believe (untested) that this does the same task:
sub fold { my $self = shift; my $array = $self->{data}; return unless $array && @$array; my %groups = map { $_ => 1 } @{$self->{groups}; my @keys = grep { $groups{$_} } qw/ date campaign /; my %h; foreach my $el ( @$array ){ my $k = join ":", @{$_}{@keys}; # This only works if the +date & campaign values do not contain ':' if( exists $h{$k} ) { $h{$k}->{views} += $el->{views}; $h{$k}->{clicks} += $el->{clicks}; }else{ $h{$k} = $el; } } @$array = sort { fold_sort($groups{campaign},$groups{date}) } values + %h; }
Note that the basic approach is to hash up on the commonality (date and/or campaign) to combine everything, then take those values and sort for final result.