Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Check if element in One Array Exist in Other Array

by Anonymous Monk
on Oct 13, 2005 at 05:48 UTC ( [id://499770]=perlquestion: print w/replies, xml ) Need Help??

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

Guys,
I want to check two array refs. Let's call it $test1 and $test2 with another fixed source array called: $source. Now what I intend to get is, whenever an element in $test1 or $test2 doesn't exist in $source. It should return false. Is there a quick way to do this?
I'm kinda stuck with my code below.
$test1 = ['A','D','B']; $test2 = ['A','C','B']; $source = ['A','B','C','A','B']; check_array($test1,$source); # Should return False # since "D" doesn't exist in # $source check_array($test2,$source); # Should return True # since all element exist in # $source check_array($test1,$source); sub check_array { my ($test,$source) = @_; OUT: foreach my $ts ( @{$test} ) { foreach my $sc (@{$source} ) { if ( $ts ne $sc ) { print "FALSE\n"; last OUT; } else { print "TRUE\n"; } } } return ; } Thanks beforehand.

Replies are listed 'Best First'.
Re: Check if element in One Array Exist in Other Array
by gopalr (Priest) on Oct 13, 2005 at 06:18 UTC

    Take a look at List::Compare::Functional Module.

    Update

    use List::Compare::Functional qw(is_LsubsetR); $test1 = ['A','D','B']; $test2 = ['A','C','B']; $source = ['A','B','C','A','B']; $result = is_LsubsetR( [ $test1, $source ] ); $result == 1 ? print "\n\$test1 is True" : print "\n\$test1 is False"; $result = is_LsubsetR( [ $test2, $source ] ); $result == 1 ? print "\n\$test2 is True" : print "\n\$test2 is False";

    OUTPUT

    $test1 is False $test2 is True

Re: Check if element in One Array Exist in Other Array
by Ovid (Cardinal) on Oct 13, 2005 at 06:20 UTC

    Unless I have seriously misunderstood you, the following should suffice.

    #!/usr/bin/perl -l use strict; use warnings; my $test1 = [ 'A', 'D', 'B' ]; my $test2 = [ 'A', 'C', 'B' ]; my $source = [ 'A', 'B', 'C', 'A', 'B' ]; print "yes 1" if check_array( $test1, $source ); # Should return Fa +lse # since "D" doesn' +t exist in # $source print "yes 2" if check_array( $test2, $source ); # Should return Tr +ue # since all elemen +t exist in # $source sub check_array { my ( $test, $source ) = @_; my %exists = map { $_ => 1 } @$source; foreach my $ts ( @{$test} ) { return if ! $exists{$ts}; } return 1; }

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Check if element in One Array Exist in Other Array
by graff (Chancellor) on Oct 13, 2005 at 06:44 UTC
    If you really are always comparing things to a single "source" array that doesn't change, you should just make it a hash instead of an array. That's basically what Ovid does in his subroutine, but doing it in the sub means you re-build the hash every time you call it.

    So, wherever you actually put values into your source array, instead of doing it this way:

    @source = ( ... );
    just do it this way:
    %source = map { $_ => undef } ( ... );
    Then your check_array sub is just the for loop:
    sub check_array { my ( $array, $source ) = @_; for ( @$array ) { return if ( ! exists( $$source{$_} )); } return 1; }
Re: Check if element in One Array Exist in Other Array
by Rajeshk (Scribe) on Oct 13, 2005 at 07:41 UTC
    $a = ['A','B','D'];
    $b = ['B','E','F'];
    $source = ['A','B','C','E','F'];
    $x = &check_array($a, $source);
    print "First Comparison   = $x\n";
    
    $x = &check_array($b, $source);
    print "Second Comparison  = $x\n";
    
    
    sub check_array{
    	my ($a, $source) = @_;
    	foreach my $elt (@$a){
    		return 1 unless(grep /$elt/, @$source);
    	}
    	return 0;
    }
    
Re: Check if element in One Array Exist in Other Array
by Skeeve (Parson) on Oct 13, 2005 at 06:10 UTC

    You bail out as soon asthe first element of @$test doesn't equal all elements of @$source.

    foreach my $ts ( @{$test} ) { foreach my $sc (@{$source} ) { if ( $ts ne $sc ) # <- You will fail here { print "FALSE\n";
    See what's compared in the line marked above:
    1. loop: 'A' ne 'A' -> okay. They are equal 2. loop: 'A' ne 'B' -> TRUE So fail...

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

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

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

    No recent polls found