in reply to Extract part of a list

one possible solution is (if i understood the question) :
use strict; my @notgood = qw(1 5 6 9); my @goodarray = qw(1 2 3 4 5 6 7 8 9); my @good; foreach my $yes (@goodarray){ if (!grep{$_ == $yes}@notgood){ push @good , $yes; } } print "@good";
but read how grep works and you'll probably find more elegant way to do this

Replies are listed 'Best First'.
Re^2: Extract part of a list
by blazar (Canon) on Jul 29, 2008 at 08:45 UTC

    I personally believe that to expand a little bit on the subject, I may mention that that code may be cast in the form of two nested greps:

    my @good = grep { my $yes = $_; !grep $_ == $yes, @notgood; } @goodarray;

    One minor inconvenience, with the inner one, is that it scans all of @notgood also when it has already found one matching element: of course we're generally not manic about this because we don't care premature optimization; but I somewhat psychologically feel uneasy with it. Then List::Util's first() comes to the rescue:

    my @good = grep { my $yes = $_; !first {$_ == $yes} @notgood; } @goodarray;

    Too bad it's not a builtin in the first place. But then 5.10 can do even better, with its smart match operator:

    my @good = grep { not @notgood ~~ $_ } @goodarray;

    And that's it!

    --
    If you can't understand the incipit, then please check the IPB Campaign.