0

I have an array containing some PCRE matching patterns (like "prefix_.*") and I'm comparing a string with all the patterns of the array. Currently, I'm working with this code:

foreach (@matchingPatterns) {
    if ("$string" =~ "$_") {
        [do something]
    }
}

This code is working well but I'm sure that there is a prettier way to do that in Perl (without any loop?).

Am I wrong? ;)

1 Answer 1

1

There's not much scope for improvement here, but I'd be more liable to write something like one of the following:

for (@matchingPatterns) {
    next if $string !~ /$_/;
    # do something
}

or

for (grep { $string =~ /$_/ } @matchingPatterns) {
    # do something
}

...both of which at least save you a few lines of code.

Sign up to request clarification or add additional context in comments.

1 Comment

While we're golfing anyway, do_something for grep {$string =~ $_} @matchingPatterns;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.