Date: 26 Sep 2001 00:35:55 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: logical comparison
Message-Id: <slrn9r28im.cr1.damian@puma.qimr.edu.au>

On Tue, 25 Sep 2001 23:37:00 GMT, John Hall said:
>$filename = 'bob';
>if ($filename eq 'james' || $filename eq 'bob') { print 'yes'; } else {
>print 'no'; }
>
>- Basically, I would like a very simple way of comparing one variable to
>several values without resorting to a regex.

A way to generalise to an arbitrary number of possible candidates:
[untested]

#!/path/to/perl -w
use strict;

my @candidates = qw(james bob jim robert);
my $filename = 'bob';

my @matches = ( grep { $_ eq $filename } @candidates );
die "Too many matches - are the candidates corrupt?\n" if @matches > 1;

print scalar @matches? 'yes' : 'no', "\n";
	#or even:
my @answer = qw(no yes);
print $answer[scalar @matches], "\n";

HTH

Cheers,
Damian
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub


