in reply to Optimize code | remove duplicates from AOA.
You are nearly there with the hash idea, but missed a simplification. Just step through your AoA in one loop and store the keys in a separate hash. If you find a duplicate, delete the Array entry (or whatever else you want to do with duplicates):
my %found; my $i=0; while ($i<@$AOA) { my $key = $A->[1] . '-' . $A->[3]; if (exists $found{$key}) { splice(@$AOA,$i,1); } else { $found{$key}=1; $i++; } }
(Untested code). If you need to do some processing of the deleted item, change above line to my $deleteditem=splice(@$AOA,$i,1);.
UPDATE: Thanks go to moritz for detecting that I forgot to increment $i. Corrected
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Optimize code | remove duplicates from AOA.
by moritz (Cardinal) on Aug 19, 2010 at 09:31 UTC | |
by jethro (Monsignor) on Aug 19, 2010 at 10:39 UTC |