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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.