> -----Original Message----- > From: Paul Kraus [mailto:[EMAIL PROTECTED]] > Sent: Friday, December 13, 2002 10:19 AM > To: 'Perl' > Subject: Conditional Array lookup > > > Is there a way to do an if or unless against an entire array. > > @exclude = .... > > #compare $soomevaraible to every element of @exclude. > unless ($somevariable eq @exclude){ > ... > }
The FAQ addresses this: perldoc -q 'How can I tell whether a list or array contains a certain element?' >From your example, it sounds like @exclude is a list of things you want to test against repeatedly. If so, turn @exclude into a hash: %exclude = map { $_=>1 } @exclude; Now you can test quickly: unless (exists $exclude{$somevar}) { print "$somevar is OK\n"; } If you only need to check once, grep() is ok: unless (grep { $_ eq $somevar } @exclude) { print "$somevar is OK\n"; } But don't do that repeatedly; use the hash instead. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]