ashish.kvarma has asked for the wisdom of the Perl Monks concerning the following question:
I was looking at some code (to get unique AOA) which I though was not very efficient. A simplified version of this code is given below.
(Please note that only index 1 and 3 need to be unique, also the result should be in same order)
use strict; use warnings; use Data::Dumper; # Only the combination of index 1 and 3 needs to be unique. my $AOA = [ [ 1, 219.00, # Need to check this element for unique 'ABC', 'Abcdefghijklmnopqrstuvwxyz', # Need to check this element fo +r unique 'def', '', ], [ 2, 219.00, 'ABC', 'Abcdefghijklmnopqrstuvwxyz', 'un', 'we', ], [ 3, 209.20, 'ABC', 'AbcdefghijklmnopqrstuvwxyzAbcdefghijk', 'udf', '', ], [ 4, 209.20, 'ABC', 'Abcdefghijklmnopqrstuvwxyz', 'und', '', ], ]; my $max = $#$AOA; my @matched_indexes; my $count = 0; for (my $j=0;$j <= $max ;$j++) { for (my $i=$j+1;$i <= $max;$i++) { if(($AOA->[$i][1] == $AOA->[$j][1]) && ($AOA->[$i][3] eq $AOA- +>[$j][3])) { $matched_indexes[$count] = $i; $count++; } } } foreach my $index(@matched_indexes) { $AOA->[$index][1] = undef; $AOA->[$index][3] = undef; } my @new_AOA; #Deleting the array elements if undef for(my $index=0;$index<=$max;$index++) { if ((defined $AOA->[$index][1]) && (defined $AOA->[$index][3])) { push(@new_AOA, $AOA->[$index]); } } # Duplicate records removed print Dumper \@new_AOA; # Expected Output #$VAR1 = [ # [ # 1, # 219, # 'ABC', # 'Abcdefghijklmnopqrstuvwxyz', # 'def', # '' # ], # [ # 3, # '209.2', # 'ABC', # 'AbcdefghijklmnopqrstuvwxyzAbcdefghijk', # 'udf', # '' # ], # [ # 4, # '209.2', # 'ABC', # 'Abcdefghijklmnopqrstuvwxyz', # 'und', # '' # ] # ];
I tired a few solutions to make it more efficient, the best I could think was:
my %HOA; my $count = 0; foreach my $A (@$AOA) { my $key = $A->[1] . '-' . $A->[3]; if (!defined $HOA{$key}) { $HOA{$key} = [$A, $count++]; } } # Records need to be in the same sequence my @new_AOA = sort { $a->[1] <=> $b->[1] }values %HOA; @new_AOA = map {$_->[0]} @new_AOA; print Dumper \@new_AOA;
Above solution gets rid of nested loop, though added new loops in form of sort and map (Which I need to maintain the same sequence as in original AOA).
Other thing which I could see is the length of keys of hash as the index 3 can have very large string. I am not sure if large keys could be any issue.
Update:Fixed some typo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Optimize code | remove duplicates from AOA.
by moritz (Cardinal) on Aug 19, 2010 at 09:07 UTC | |
by jonc (Beadle) on Jul 09, 2011 at 16:37 UTC | |
Re: Optimize code | remove duplicates from AOA.
by jethro (Monsignor) on Aug 19, 2010 at 09:09 UTC | |
by moritz (Cardinal) on Aug 19, 2010 at 09:31 UTC | |
by jethro (Monsignor) on Aug 19, 2010 at 10:39 UTC |