From: Alok Bhatt <[EMAIL PROTECTED]>
> --- Jason Dusek <[EMAIL PROTECTED]> wrote:
> > Hi List,
> > 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])$/);
> > }
> Hi,
> try this
> my @suffix=(txt, jpg, tif);
> my $pattern= join "|", @suffix;
>
> foreach (@ARGV) {
> print if /\.$pattern$/;
> }
This would match even foo.notatif, you have to add braces into the
regexp.
print if /\.(?:$pattern)$/;
(The ?: makes the braces non-capturing. See perldoc perlre)
You will also either want to use the /o option to compile the regexp
just once or use the qr// operator:
my $pattern= join "|", @suffix;
foreach (@ARGV) {
print if /\.$pattern$/o;
}
or
my $pattern = join "|", @suffix;
$pattern = qr/\.$pattern$/; # compile the regexp
foreach (@ARGV) {
print if $_ =~ $pattern;
}
HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>