That really depends. If @ARRAY is only a couple of elements, you're fine:
$x = 3;
$y = 7;
@array = ( 0, 3, 7 );
if ( grep { $_ == $x } @array and grep { $_ == $y } @array ) {
print "Good\n";
} else {
print "Bad\n"
}
However, if your array is sizable (or if you are iterating over this construct, using grep is not a good idea. grep forces you to check every value in @ARRAY, but a foreach loop can be exited as soon as a match is found, thus being more efficient.
Update: I tossed this code out quickly, so I realize that I may not have gotten the full intent of what you wanted. You mentioned three arg values (I missed that at first), but then went on to state that there are things you want to do if the first two args evaluate as true. Is the third arg value even relevant to your question? I'm missing something here. If it's not relevant, then I think my snippet answers your question. Otherwise, could you clarify?
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just go the the link and check out our stats. | [reply] [d/l] |
I'm still not clear on exactly what you want to do (part of this might be your code; $sec_arg next to $zero_arg suggests that $sec_arg is actually the *third* arg (where's $first_arg?)
I'll just deal with the logic:
if (FOUND FIRST ARG) {
#look for second arg; report true if you find it
} else {
#say "didn't find first arg" and exit
}
Whether or not you should use a subroutine depends on what the code's doing, and whether you'll need to do similar things in the future a lot; putting it in a sub is a great way to reuse code.
Other factors are readability and maintainability: if you want the maintainer to know exactly what's going on at a glance, you could use a sub with a sensible name.
I'm sure there are others 'round here who can expand on these points.
Philosophy can be made out of anything. Or less -- Jerry A. Fodor | [reply] [d/l] [select] |
You might consider a recursive function if you'd like to look ahead to when you need to check four, or four hundred, files...
Or a while loop might be just what you want (and without the overhead of a function).
my $command = pop @ARRAY;
while (shift @ARRAY) {
die ("Error: " . $!) unless -e;
}
# run your command here -- exec $command, I assume
This does take a performance hit because of the shift, so if speed is paramount you could just as easily use a for{} loop.
Hope that answers the question. | [reply] [d/l] |
I need some clarification:
- What do you mean by "arg value"... do you mean the arguments at a subroutine like so:
sub y {
my @arg = @_;
print join ':', (@arg[0..2]);
}
- What do you mean "compare against respective files"? How do you want to compare them and what is in each "arg value"?
- You never mention the purpose or contents of the 3rd "arg value"
In the large, it sounds like you are doing this:
<code>
sub y {
my @arg = @_;
if (compare($arg[0],$file[0]) {
if (compare($arg1,$file1) {
print '...';
}
} else {
print 'dont care about the second';
}
}
<code>
The compare() pseudo-function above could be a short as Perl function/operator or as long as subroutine or method call depending on how you clarify the definition of your problem.
| [reply] [d/l] |