Jason Dusek wrote:
> 
> Hi List,

Hello,

> Let's say I want to know if anything in @ARGV has one of a certain list
> of suffixes in it. So I write:
> 
>    foreach (@ARGV) {
>      print if (/\.(fish|foul)$/);
>    }
> 
> But if I have a long list of suffixes, then I would like to store the
> suffixes in an array, and then evaluate the array in my regular
> expression. What does this? I've tried:
> 
>    @SUFF = (fish,foul);
>    foreach (@ARGV) {
>      print if (/\.(@SUFFIXES)$/);
>    }
> 
> and also:
> 
>    @SUFF = (fish,foul);
>    foreach (@ARGV) {
>      print if (/\.(@[EMAIL PROTECTED])$/);
>    }
> 
> but I can't get it to work.


To make sure that it works correctly you have to join each suffix with a
'|' character between them and ensure that any special regular
expression characters are escaped.  Also, using qr// to pre-compile the
regexp may help.

my @SUFF = qw( fish foul );
my $match = join '|', map "\Q$_", @SUFF;
foreach ( @ARGV ) {
  print if /\.(?:$match)$/;
}

Or better:

my @SUFF = qw( fish foul );
my $match = qr/\.(?:@{[ join '|', map "\Q$_", @SUFF ]})$/;
foreach ( @ARGV ) {
  print if /$match/;
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to