BarMeister has asked for the wisdom of the Perl Monks concerning the following question:
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 :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array Folding
by davidrw (Prior) on Mar 05, 2006 at 00:54 UTC | |
|
Re: Array Folding
by borisz (Canon) on Mar 05, 2006 at 01:04 UTC | |
by BarMeister (Beadle) on Mar 07, 2006 at 14:53 UTC | |
|
Re: Array Folding
by davidrw (Prior) on Mar 05, 2006 at 00:35 UTC |