Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How can I find the union/difference/intersection of two arrays?

by Dev Null (Acolyte)
on Jun 07, 2017 at 15:56 UTC ( [id://1192290]=note: print w/replies, xml ) Need Help??


in reply to How can I find the union/difference/intersection of two arrays?

This is inefficient, but I use it for intersection when I know I'm guaranteed small list sizes, and it's more valuable / pleasing to me to have a one-line solution:

my @isect = map { my $b = $_; grep { $_ eq $b } @a } @b;
  • Comment on Re: How can I find the union/difference/intersection of two arrays?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How can I find the union/difference/intersection of two arrays?
by Anonymous Monk on Nov 16, 2017 at 16:10 UTC

    the problem with that method is that your @isect can contain identical elements

    for example,if @a=(1,2,3,4,2) and @b=(1,2,4), your @isect will be: (1,2,2,4)

    before using this method you have to make sure each element is unique in both tab

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found