in reply to Check if element in One Array Exist in Other Array
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.
|
|---|