Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Selective Check of Multiple Arrays With Single Construct

by blokhead (Monsignor)
on Apr 14, 2007 at 15:52 UTC ( [id://610074]=note: print w/replies, xml ) Need Help??


in reply to Selective Check of Multiple Arrays With Single Construct

Instead of doing doubly-nested loops where you check everything in A against everything in B, you can use the %all hash to do it all in one pass:
use Data::Dumper; my @arA = qw( lion tiger dog cat snake); my @arB = qw( tiger dragon lion); my @arC = qw(dog phoenix); my %all; @all{@arA} = (); for (@arB) { next unless exists $all{$_}; push @{$all{$_}}, "$_ - from array B"; } for (@arC) { next unless exists $all{$_}; push @{$all{$_}}, "$_ - from array C"; } print Dumper \%all;
If something from array A was not found in the other arrays, it will exist in %all with an undefined value. You can grep these out at the end if you prefer.

It would be easy to make this extensible (for arrays D, E, F, etc) if @arB, @arC were named like @{$arrays{B}} and @{$arrays{C}}. I'll leave that to you.

blokhead

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-16 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found