in reply to Re: Extract part of a list
in thread Extract part of a list
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!
|
|---|