Is there a way I can avoid using this for multiple pattern checks?
Can I tore all the patterns in an array and check if it matches any pattern in the pattern array? Please consider the case when I have more than 20 pattern strings.
if( ($_=~ /.*\.so$/)
|| ($_=~ /.*_mdb\.v$/)
|| ($_=~ /.*daidir/)
|| ($_=~ /\.__solver_cache__/)
|| ($_=~ /csrc/)
|| ($_=~ /csrc\.vmc/)
|| ($_=~ /gensimv/)
){
...
}
If you can use Perl version 5.10, then there is a really easy way to do that. Just use the new smart match (~~) operator.
use warnings;
use strict;
use 5.10.1;
my @matches = (
qr/.*\.so$/,
qr/.*_mdb\.v$/,
qr/.*daidir/,
qr/\.__solver_cache__/,
qr/csrc/,
qr/csrc\.vmc/,
qr/gensimv/,
);
if( $_ ~~ @matches ){
...
}
If you can't use Perl 5.10, then I would use List::MoreUtils::any.
use warnings;
use strict;
use List::MoreUtils qw'any';
my @matches = (
# same as above
);
my $test = $_; # copy to a named variable
if( any { $test =~ $_ } @matches ){
...
}
Another pre-Perl 5.10 option is Regexp::Assemble, which will take a list of patterns and combine them into a single regex that will test all of the original conditions at once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With