in reply to File pairing from different directories
Your first approach is almost there, except that you're looking through the wrong list, repeatedly, which is what drives up your runtime. Basically, what you want is called the "symmetric difference" between two lists, and perlfaq4 (or perldoc -q difference) tells you the common approach.
All that's left for you to do is to canonicalize the names to a common form. For example in your case, abc_12342_tick could be a good common form from which both forms can be derived.
The code in perlfaq4 is:
@union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$element}++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@differen +ce }, $element; }
Personally, I like to know whether an element is only in the left or only in the right side of the comparison, so I usually use code like this:
@union = @intersection = @left = @right = (); %count = (); my %left; @left{ @array1 } = 1 x @array; my %common; foreach $element (@array2) { if (exists $left{ $element }) { $common{ $element } = delete $left{ $element }; } else { $right{ $element } = 1; }; }; print "Keys only on the left side:\n"; for (keys %left) { print "$_\n"; }; print "Keys only on the right side:\n"; for (keys %right) { print "$_\n"; }; print "Keys found on both sides:\n"; for (keys %common) { print "$_\n"; };
This only works if you don't have duplicates - the code for handling multiple keys isn't difficult but larger and detracts from the main logic.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: File pairing from different directories
by Zoop (Acolyte) on Sep 08, 2009 at 13:30 UTC | |
by Corion (Patriarch) on Sep 08, 2009 at 13:35 UTC |