what's wrong with this OR statement syntax

L

levinepw

Hi,

This code does not work:

----------------------------------------------
my $color='red';

if($color eq ('blue' || 'red' || 'green'))
{
print "cool\n";
}
----------------------------------------------

I wanted a shorthand to

my $color='red';

if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
{
print "cool\n";
}

Can someone tell me why my attempt doesn't work & if there is a
simpler way to write the statement.

Thanks
 
J

Jeremy Henty

----------------------------------------------
my $color='red';

if($color eq ('blue' || 'red' || 'green'))
{
print "cool\n";
}

Because 'blue' || 'red' || 'green' is an expression whose value is
'blue', so your code is equivalent to "if($color eq 'blue') { ...".
& if there is a simpler way to write the statement.

My first thought would be to put the values in a hash and look them
up.

my %colors = ( blue => 1, red => 1, green => 1 );
if ($colors{$color}) { ...

There's probably a cooler way to do it but I wouldn't know, I'm not
even JAPH.

Regards,

Jeremy Henty
 
M

Manish

[snip]

You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.

Regards,
Manish
 
A

anno4000

Hi,

This code does not work:

----------------------------------------------
my $color='red';

if($color eq ('blue' || 'red' || 'green'))
{
print "cool\n";
}
----------------------------------------------

I wanted a shorthand to

my $color='red';

if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
{
print "cool\n";
}

Can someone tell me why my attempt doesn't work & if there is a
simpler way to write the statement.

What makes you think it should work?

Just because a short-hand is common in everyday mathematical notation
doesn't mean it is common in programming languages. In fact, there
are few, if any, languages that implement that. Perl's approximation
to such a notation is in the module Quantum::Superpositions, available
on CPAN.

For an ad-hoc approach you could set up a hash, like this:

my %is_cool_color = map +( $_ => 1), qw( blue red green);

Then you can say

if ( $is_cool_color{ $color} ) {
print "cool\n";
}

and similar.

Anno
 
M

Mark Clements

Manish said:
[snip]

You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.

You haven't tested this.

mark@owl:~$ cat testre2.pl
#!/usr/bin/perl

use strict;
use warnings;

while(my $item = <DATA>){
chomp $item;
my $match = $item =~ /[red|blue|green]/?"yes":"no";
print "$item => $match\n";
}
__END__
blue
red
green
white
yellow
mark@owl:~$ perl testre2.pl
blue => yes
red => yes
green => yes
white => yes
yellow => yes

/^(red|blue|green)$/

works as the regex.

Mark
 
D

Dan Mercer

: [snip]
:
: You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.

That doesn't do what you think it does. Oh, it matches red, blue and
green alright. It also matches black and '|'. You have set up a
character class so that $color matches if it contains any
character in the class. What you want is:

if ($color =~ /^(red|blue|green)$/)

You have to anchor the expression so it only matches red or blue or green.
The choices have to be grouped by parentheses, because /^red|blue|green$/
matches any string(e.g. redolent) that begins with red, any string that
contains blue and any string that ends in green.

Dan Mercer

:
: Regards,
: Manish
:
 
M

Mirco Wahab

I wanted a shorthand to
my $color='red';
if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
{
print "cool\n";
}
Can someone tell me why my attempt doesn't work & if there is a
simpler way to write the statement.

There have been some examples,
so I'll add another one:

...

sub IS { grep /\b$_[0]\b/, @{$_[1]} }
sub IN { [@_] }


my $color='red';

if( IS $color, IN qw'blue red green' ) {
print "cool\n";
}

...


Regards

Mirco
 
M

Manish

: [snip]
:
: You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.

That doesn't do what you think it does. Oh, it matches red, blue and
green alright. It also matches black and '|'. You have set up a
character class so that $color matches if it contains any
character in the class. What you want is:

if ($color =~ /^(red|blue|green)$/)

You have to anchor the expression so it only matches red or blue or green.
The choices have to be grouped by parentheses, because /^red|blue|green$/
matches any string(e.g. redolent) that begins with red, any string that
contains blue and any string that ends in green.

Dan Mercer

:
: Regards,
: Manish
:

Yeap, my mistake. Thanks for pointing it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,089
Members
48,773
Latest member
Kaybee

Latest Threads

Top