in reply to Re: Easy one: removing elements of one array from another...
in thread Easy one: removing elements of one array from another...

Alternatively, one could use delete. This code also removes any duplicates from @myarray.
#!/opt/perl/bin/perl -w use strict; my @badwords = qw /jerk store king/; my @myarray = qw /jerry wash king kong store when jerk/; @@ {@myarray} = (); delete @@ {@badwords}; @myarray = keys (%@); print "@myarray\n"; __END__

-- Abigail

Replies are listed 'Best First'.
Re: Re: Easy one: removing elements of one array from another...
by indapa (Monk) on Jun 28, 2001 at 20:33 UTC
    Taken from Perl Cookbook 4.7:

    #!/apps/bin/perl -w use strict; # Taken from Perl Cookbook 4.7 Finding Elements in One Array but # not another my %seen = (); # look up table my @goodWords; # words you want my @badWords=("jerk","store","king"); my @myArray=("jerry","wash","king","kong","store","when","jerk"); @seen{@badWords} = (); # build lookup table my $item; foreach $item (@myArray) { push (@goodWords, $item) unless exists $seen{$item}; } # print the contents of goodWords foreach (@goodWords) { print "$_\t"; } print "\n";