Re: How to Do Multiple OR in For Loops
by davido (Cardinal) on Apr 13, 2011 at 01:44 UTC
|
my $tp = 0;
my $fp = 0;
for( $pred, $pred1, $pred2, $pred3, $pred4, $pred5, $pred6 ) {
if( $myhash{$_} ) {
$tp++;
} else {
$fp++;
}
}
You might consider avoiding all those variable names with numbers. Just drop the values into an array called @pred in the first place. Then it would look like:
for( @pred ) {
.........
| [reply] [d/l] [select] |
Re: How to Do Multiple OR in For Loops
by ikegami (Patriarch) on Apr 13, 2011 at 01:54 UTC
|
my $tp = grep { $myhash{ $_ } } @pred;
If the boolean needs to be formatted into '0' and '1' for output, that's the output code's job, and it can be done there trivially using « ? '1' : '0'».
Using "for", it would look like
my $tp = 0;
for ( @pred ) {
if ( $myhash{ $_ } ) {
$tp = 1;
last;
}
}
or
my $tp = 0;
for ( @pred ) {
last if $tp = $myhash{ $_ };
}
| [reply] [d/l] [select] |
Re: How to Do Multiple OR in For Loops
by LanX (Saint) on Apr 13, 2011 at 02:16 UTC
|
List::Util is core, try importing reduce();
untested: if ( reduce { $a||$b } @myhash{@allpreds} ) {...}
UPDATED:
tested and works!
If you dont like the hashslice you can also use map {$myhash{$_}} @allpreds instead. | [reply] [d/l] [select] |
Re: How to Do Multiple OR in For Loops
by toolic (Bishop) on Apr 13, 2011 at 01:44 UTC
|
my $tp = 0;
my @allpreds = ($pred,$pred1,$pred2,$pred3,$pred4,$pred5,$pred6);
foreach my $allpred ( @allpreds ) {
if ( $myhash{$allpred} ) {
$tp = 1;
last;
}
}
my $fp = ($tp == 1) ? 0 : 1;
| [reply] [d/l] |
|
my $fp = !!! $tp;
| [reply] [d/l] |
|
!!!$tp is the same as just !$tp, and it's not equivalent to $tp ? 0 : 1.
$ perl -E'$tp=1; say "[", !$tp, "]";'
[]
$ perl -E'$tp=1; say "[", $tp ? 0 : 1, "]";'
[0]
| [reply] [d/l] [select] |
|
|
Re: How to Do Multiple OR in For Loops
by Anonymous Monk on Apr 13, 2011 at 01:38 UTC
|
my $tp=0;
my $fp=0;
my @allpreds = ($pred,$pred1,$pred2,$pred3,$pred4,$pred5);
foreach my $allpred ( @allpreds ) {
if ( $myhash{$allpred} ) {
$tp++;
}
}
if ( !myhash{$pred} ) {
$fp++;
}
__END__
Can't call method "myhash" on an undefined value at - line 13.
| [reply] [d/l] |
Re: How to Do Multiple OR in For Loops
by Anonymous Monk on Apr 13, 2011 at 01:41 UTC
|
if ( grep { exists $myhash{ $_ } } @pred ){
$tp++;
} else {
$fp++;
}
| [reply] [d/l] |
Re: How to Do Multiple OR in For Loops
by JavaFan (Canon) on Apr 13, 2011 at 06:40 UTC
|
my $tp=0;
my $fp=0;
my @allpreds = ($pred,$pred1,$pred2,$pred3,$pred4,$pred5);
$fp++;
foreach my $allpred (@allpreds) {
if ($myhash{$allpred}) {
$tp++;
$fp--;
last;
}
}
| [reply] [d/l] |
Re: How to Do Multiple OR in For Loops
by Marshall (Canon) on Apr 13, 2011 at 09:17 UTC
|
Your "if" statement is very efficient. There is no need to change it unless you need to make a major expansion of the $predX variables, probably with a @pred array. Otherwise, I see no need to re-visit and change working code.
Do not mistake fewer lines of code for greater efficiency.
However,
ikegami's code looks to me to be short, efficient and uses the power of the language, ie. grep. | [reply] |
|
Note though that || short circuits (doesn't evaluate more clauses than needed to calculate the answer), where as grep will always perform all checks.
Of course, it's not very common to have so many clauses that it matters. You may need hundreds, if not thousands of clauses to make a significant difference (and then only if there's a true clause "early" enough).
| [reply] [d/l] [select] |
|
I was wondering if the short circuit could cause side effects. But in this case there is no risk of auto-vivication.
With side-effects I'd rather prefer a non short circuit solution for clarity.
If a short circuit effect is side effects areš wanted, it should be done explicitly.
1) corrected bad phrasing!
| [reply] |
|
|