Anyone have a better way of doing this? The fold_sort function just orders the array based on how were are grouping the elements. Thanks, Kennysub fold { my $self = shift; return if ! @{$self->{data}}; my $array = $self->{data}; my $date_grouped = 0; my $campaign_grouped = 0; foreach(@{$self->{groups}}) { $date_grouped = 1 if $_ eq 'date'; $campaign_grouped = 1 if $_ eq 'campaign'; } my $merge_index = 0; my $points_needed = $campaign_grouped + $date_grouped; @$array = sort {fold_sort($campaign_grouped,$date_grouped)} @$arra +y; for(my $current_index=0; $current_index<@$array; $current_index++) + { my $el = $array->[$current_index]; if($merge_index == $current_index) { next; } #compare current element to merge element my $match = 0; my $merge = $array->[$merge_index]; $match++ if $date_grouped && $el->{date} eq $merge->{date}; $match++ if $campaign_grouped && $el->{campaign_id} == $merge->{campaign_id}; #if we can fold the array elements then do so and remove the #current element if($match == $points_needed) { $merge->{views} += $el->{views}; $merge->{clicks} += $el->{clicks}; my $del = splice(@$array,$current_index,1); $current_index--; } else { $merge_index++; } } }
PS - I was not sure of what to call this so I figured "Array Folding" was a good enough term...if there is another term that people use let me know please :-)
In reply to Array Folding by BarMeister
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |