Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Grepping on array

by hotshot (Prior)
on May 19, 2004 at 12:54 UTC ( #354583=perlquestion: print w/replies, xml ) Need Help??
hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi all!

I have two arrays, I need to get all the values values in one array that DON'T appear in the second array, example:
my @arr1 = qw(a b c d e f g h); my @arr2 = qw(d a e); # needed array @result = qw(b c f g h);
Anyone?

Replies are listed 'Best First'.
Re: Grepping on array
by diotalevi (Canon) on May 19, 2004 at 13:01 UTC
    I would feel silly quoting the documentation. Execute perldoc -q "difference of two arrays" to see the standard answer. This is in perlfaq4, btw.
Re: Grepping on array
by ctilmes (Priest) on May 19, 2004 at 13:05 UTC
    You can also use List::Compare to do this sort of thing:
    my @arr1 = qw(a b c d e f g h); my @arr2 = qw(d a e); my @result = List::Compare->new(\@arr1, \@arr2)->get_Lonly; # @result = qw(b c f g h);
Re: Grepping on array
by Abigail-II (Bishop) on May 19, 2004 at 13:01 UTC
Re: Grepping on array
by herveus (Parson) on May 19, 2004 at 13:08 UTC
    Howdy!

    my %first = map { $_ => 1 } @arr2; @result = grep !exists $first{$_}, @arr1;

    %first is a hash whose keys are the distinct values in @arr2. The grep returns a list of the values in @arr1 that don't exist in %first, thus eliminating values in @arr2.

    yours,
    Michael
Re: Grepping on array
by monkey_boy (Priest) on May 19, 2004 at 15:12 UTC

    Set::scalar
    esp. usefull if your manipulations get more complex.

    I should really do something about this apathy ... but i just cant be bothered
Re: Grepping on array
by dragonchild (Archbishop) on May 19, 2004 at 14:07 UTC
    This is a very basic set arithmetic question. (You're looking for the result set when subtracting @arr2 from @arr1.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://354583]
Approved by herveus
help
Chatterbox?
[MidLifeXis]: Most likely it is a code that some undocumented system, hidden behind layers of IT, deep in the bowels of the building under the machine room floor, reads that code to keep a presence switch from going off. :-b
[MidLifeXis]: I think I forgot "running on a farm of commodore 64, vic 20s, trs 80s, and apple ]|[e systems"
[GotToBTru]: oh I know what it is .. but it is a number only slightly useful to me and of no possible use to our customer
[MidLifeXis]: Whew - you just saved the free world. <o)
[GotToBTru]: i guess it's a placeholder, the code will only fill it in if there is nothing else to use
[GotToBTru]: but then .. if you have nothing to say, why not say nothing?
[MidLifeXis]: I have a user who has a lot of say on how some of our processes work that abhors significant blanks. Perhaps that is a part of it. A not-so-obvious "this space intentionally left blank" indicator.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-01-20 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (176 votes). Check out past polls.