in reply to Direct regex vs. regex stored in a variable

Instead of

> my $wanted_regex = qr/B/;

> ... grep $wanted_regex, @cities;

please try

or

or
Explanation:

$wanted_regex is a variable holding an Regexp object (at runtime) not an operator.

And objects resp. their references are always true, that's why you get the whole list.

You need to specify at least one operator for the parser to know what you really want.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

update

tested code and expanded explanation

Replies are listed 'Best First'.
Re^2: Direct regex vs. regex stored in a variable
by richard90522 (Novice) on Apr 16, 2023 at 07:12 UTC
    Thank you very much! Everything is working now with your friendly help.