in reply to Re: comparing arrays
in thread comparing arrays

Hi, Sorry I thought I had explained it. I want to remove just one copy of the duplicate pair e.g. one value from each array - it doesn't matter which array the values are removed from. I dont see how a hash would work - it would help extract the unique values in each array, but how could I use it to keep one copy of the duplicate values? Thanks!

Replies are listed 'Best First'.
Re^3: comparing arrays
by sauoq (Abbot) on Dec 16, 2004 at 01:55 UTC
    it doesn't matter which array the values are removed from.

    In that case, it is very simple. You iterate over one array and rebuild it. If a value shows up in the second array, you just ignore it as you are rebuilding. Use a hash to store the values so that lookup is fast...

    my @array1 = (1, 2, 3, 4, 5); my @array2 = (2, 4, 6, 8, 10); my %hash = map {$_=>1} @array2; @array1 = grep { not exists $hash{$_} } @array1; print "@array1\n";

    -sauoq
    "My two cents aren't worth a dime.";
    

      A hash slice would (I guess) be more efficient (insted of the map-statement).

      my %hash; @hash{@array2};

      This code will set for all the elements in @array2 the value in the hash to undef. And later on you can see if it exists by using the exists (or not exists) function (as done in your code).