[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] testing



At 3:54 PM -0800 12/14/99, Nicholas G. Thornton wrote:
>in the test part of an if statement is there a way to simplify...
>     if ($value == $a or $value == $b or  $value == $c or $value == $d or ...)
>...into...
>     if ($value == ($a or $b or $c or $d or...))
>...or something similar? I can't seem to find it in the Camel book.


Continuation of my previous posting on this topic:

If you _do_ have to get an exact match, numerical (==) or string 
(eq), with one of a batch of variables (or elements of a list), 
here's a nice trick: Populate the keys of a hash with your variables, 
and then check for the existence of a hash element whose key is your 
test value. This sounds roundabout, but sometimes does just what's 
needed, especially when you don't know ahead of time the nature or 
number of your variables. Keep track if you've used references, or 
you'll mostly get the opposite of what you expect.

[ NOTE: Too bad we can't do this:
    keys %test = ($a, $b, $c, $d);  ]


#!perl -wl

my %test;

foreach (qw(a b c d)) {$test{$_}++}

TRY: for (;;) {
	print "Check for which letter?";
	$value = <STDIN>;
	chomp $value;
	last TRY if $value eq 'qq';
	$answer = exists($test{$value}) ? "Yay!" : "Boo!";
	print $answer;
}
print 'Goodbye.';

HTH

1;


- Bruce

__Bruce_Van_Allen___bva@cruzio.com__Santa_Cruz_CA__

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org