cjhutton
cjhutton

Reputation: 91

Deleting elements in a perl array without index

I have an array which contains several strings. There is another array which contains strings which may or may not be contained in the first array.

What is the best way to remove any instances of a string in array b from the strings in array a. I have tried to use grep as in the example below but it completely empties the array.

my @Aray = ('Chris', 'John', 'Paul', 'Max', 'Jim', 'John');
my @Bray = ('Chris', 'John');
foreach $name (@Bray){
    @Aray = grep {$_ != $name} @Aray;
}

This would result in no elements left in @Aray, opposed to 'Paul', 'Max', 'Jim'

I also tried with a traditional for loop and indexing each name in @Bray with the same result.

Upvotes: 0

Views: 2787

Answers (1)

Hunter McMillen
Hunter McMillen

Reputation: 61550

@choroba shows above how to solve the specific problem you are asking.

Deleting elements of one array based on the elements on another is a common problem in Perl, and there is a useful idiom that speeds up such operations. Specifically:

  1. Place the elements you want to delete (Array B) in a Hash H.
  2. Loop over Array A and test whether any of its elements are in Hash H, if so delete them, otherwise keep them

This method has the benefit of only reading the delete array (Array B) a single time instead of for every element of Array A

my @Aray = ('Chris', 'John', 'Paul', 'Max', 'Jim', 'John');
my @Bray = ('Chris', 'John');

my %to_delete = map { $_ => 1 } @Bray;
@Aray = grep { ! $to_delete{$_} } @Aray;

Upvotes: 6

Related Questions