Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Finding wether all elements of one array are in another

by tsk1979 (Scribe)
on Mar 31, 2006 at 09:09 UTC ( [id://540417]=perlquestion: print w/replies, xml ) Need Help??

tsk1979 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, consider an array @a and an array @b. I want to find out wether all the elements of @a are there in @b for example @a = (-true, -depth=1) @b = (-search, -depth=1, -true, -expand_all...) In the above case the command should return true, all the elements in @a are in @b. I implemeted this foreach
foreach $elem_b(@b) { foreach $elem_a(@a) { . . } }
This is a bit tedious method. Could you suggest a short method or something more elegent

Replies are listed 'Best First'.
Re: Finding whether all elements of one array are in another
by davorg (Chancellor) on Mar 31, 2006 at 09:19 UTC
Re: Finding wether all elements of one array are in another
by gopalr (Priest) on Mar 31, 2006 at 09:50 UTC

    Hi tsk1979 ,

    There is a module List::Compare will do that work.

    use strict; use warnings; use List::Compare; my @a = qw(-true -depth=1); my @b = qw(-search -depth=1 -true -expand_all) ; my $lc = List::Compare->new( \@a, \@b ); my @a_only = $lc->get_Lonly; my @b_only = $lc->get_Ronly; print join( ', ', @a_only ), "\n"; # 1, 2 @a_only ? print "@a_only" : print "\nAll Matched";
Re: Finding wether all elements of one array are in another
by Samy_rio (Vicar) on Mar 31, 2006 at 09:24 UTC

    Hi tsk1979, Try this,

    use strict; use warnings; use Array::Compare; my @ar1 = qw(-true -depth=1); my @ar2 = qw(-search -depth=1 -true -expand_all); my $comp = Array::Compare->new(); if ($comp->compare(\@ar1, \@ar2)){print "Same Array OK";}else{print " +Different Array OK";}

    Updated davorg solution is correct. compare function compare the arrays in order.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      Looks like order isn't significant, so you might be better off using the perm method rather than the compare method.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: Finding wether all elements of one array are in another
by tirwhan (Abbot) on Mar 31, 2006 at 09:21 UTC

    Using a temporary hash and List::MoreUtils:

    use List::MoreUtils qw(all); use strict; use warnings; my @partial = qw(-true -depth=1); my @complete = qw (-search -depth=1 -true -expand_all); my %temp; @temp{@complete} = @complete; print "Yep\n" if all { exists $temp{$_} } @partial;

    All dogma is stupid.

      I don't think you need the module. Remember that all positive is the same as none negative.

      my %temp; @temp{@complete} = @complete; print "Yep\n" unless grep { ! exists $temp{$_} } @partial;
      We're building the house of the future together.
Re: Finding wether all elements of one array are in another
by pKai (Priest) on Mar 31, 2006 at 11:47 UTC
    use strict; use warnings; my @a = qw(-true -depth=1); my @b = qw(-search -depth=1 -true -expand_all); print '@b contains @a' if @a == grep defined, @{{map {$_=>$_} @b}}{@a} +;

    O. K., I'm not too serious about proposing this. Though it meets my sense of elegance (somehow).

    Also note that no counting takes place: If an element appears more than once in @a they will be covered by a single (identical) entry in @b for acknowledging their containment.

Re: Finding wether all elements of one array are in another
by unobe (Scribe) on Apr 02, 2006 at 05:16 UTC
    Eigenstates, baby! ;-)
    use Quantum::Superpositions; @a = qw(-true -depth=1); @b = qw(-search -depth=1 -true -expand_all...); @c = qw(-true -depth=1 -height); print "right" unless eigenstates(any(@a) ne all(@b)); print "wrong" unless eigenstates(any(@c) ne all(@b));
    Output:
    right

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found