PDA

View Full Version : really stupid perl question...logical or



badperson
December 9th, 2008, 09:24 PM
Hi,

Is there a shorter way to write the if statement in this script?

print "enter a number: ";
chomp (my $val = <STDIN>);


if( $val == 4 || $val == 25 || $val == 324){
print "$val is matched\n";
} else {
print "$val is not enumerated\n";
}

something along the lines of

if($val == (4||25||324))

I've tried both the single and double bar operators; the single bar doesn't match any, and the double only matches the first one.

Makes me realize I'm not fully understanding how precedence works, also the difference between logical and bitwise or.
thanks,
bp

rbprogrammer
December 9th, 2008, 09:30 PM
Hi,

Is there a shorter way to write the if statement in this script?

print "enter a number: ";
chomp (my $val = <STDIN>);


if( $val == 4 || $val == 25 || $val == 324){
print "$val is matched\n";
} else {
print "$val is not enumerated\n";
}

something along the lines of

if($val == (4||25||324))

I've tried both the single and double bar operators; the single bar doesn't match any, and the double only matches the first one.

Makes me realize I'm not fully understanding how precedence works, also the difference between logical and bitwise or.
thanks,
bp

As far as my understanding of Perl is, that double bars || means "boolean or", while single bars | means "bitwise or". Secondly, you cannot compress the boolean "or" line. That's just the way it is.

gjoellee
December 9th, 2008, 09:36 PM
PLEASE: No questions are stupid, except for the following question: "Is this question stupid?"

Reiger
December 9th, 2008, 09:44 PM
Not being very knowledgeable about Perl but in this case you can compress the expression *slightly* by using a regex instead (of course it doesn't improve readability nor performance of your code, but hey: it's perl! :p)



if($val =~ /^(25)|((32)?4)$/)


But really that's overkill

Martin Witte
December 9th, 2008, 10:11 PM
Something like this might do:


my @list = qw(4 25 324);
if ( grep( /^$val$/, @list) )
{
print "Found\n";
}

badperson
December 9th, 2008, 10:22 PM
that makes sense...
I'm wondering, why doesn't the boolean or work there? (4||25||324)

I mean, that is a boolean evaluation, right?
bp

Reiger
December 10th, 2008, 12:03 AM
Well that is a litteral value ;)

4 is a constant
so is 25
and 324.

So the result we'd get is 4 || 25 || 324 which if I'm not mistaken would evaluate to (from casting to booleans, if Perl does this) true || true || true which renders true.

Now what do we have left then?

if($val == true)