in reply to Re^2: comparing arrays
in thread comparing arrays

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.";

Replies are listed 'Best First'.
Re^4: comparing arrays
by Animator (Hermit) on Dec 16, 2004 at 15:16 UTC

    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).