Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

At the moment I have something like:
elsif (defined $chosen_C && $chosen_C !=~ /^\*.{36}\;$EXEC_block\;/){ print OUTPUT_FILE $line_of_concern; }

But what I really want to do is substitute $EXEC_block with ...... 'any element of the array: @EXEC_block'. How would I do this within a regular expression like this?

Also could somebody please explain to me why Perl does not allow !=~.

Replies are listed 'Best First'.
Re: using an array in a regex
by ikegami (Patriarch) on Jan 16, 2006 at 19:08 UTC

    But what I really want to do is substitute $EXEC_block with ...... 'any element of the array: @EXEC_block'.

    use Regexp::List (); $EXEC_block = Regexp::List->new->list2re(@EXEC_block); ...

    Reference: Regexp::List

    please explain to me why Perl does not allow !=~.

    It's !($var =~ $re) or $var !~ $re.

    Reference: perlop

      Nice answer, and an informative one. In fact generally one does this by hand joining on "|" - and probably forgetting to \Q...\E (or quotemeta). However it is worth mentioning that the OP may also do like most mortals:

      for my $EXEC_block (@EXEC_block) { last if s/...//; # since we don't want to try again after we succeeded! }

      PS: shameless self ad - (slightly) related topic!

Re: using an array in a regex
by GrandFather (Saint) on Jan 16, 2006 at 19:19 UTC

    To answer your second question first (because it is well stated and understandable): it should be !~

    Your main question is unclear. Can you post a self contained code fragment that demonstrates the problem you are trying to solve? Do you mean that you want to concatentate a number of strings to generate one big string to match, or do you want the match to succeed if any of a number of strings match (!~ seems unlikely in that case), or do you mean that you wish to select one array element as the match string?

    Here's some code illustrating the last case:

    use strict; use warnings; my $chosen = 'some text'; my @array = (qr'some', qr'more', qr'text'); my $index = 1; if (defined $chosen && $chosen !~ /$array[$index]/) { print "\$array[$index] matches\n"; }

    DWIM is Perl's answer to Gödel
Re: using an array in a regex
by holli (Abbot) on Jan 16, 2006 at 19:52 UTC
    You will have to precompile your regex someplace before the conditon, basically like
    # @a holds a list of string my @a = ("a", "b", "c", "/", "\[", "\""); #create a regex that matches every element in the array my $re = do { qr/(@{[join ("|", map { quotemeta } @a)]})/ }; die "not a regex!" unless ref($re) eq "Regexp";
    You can put that $re into your regex like so
    #you don't need to escape ; ... $chosen_C !=~ /^\*.{36};$re;/ ...
    or you can put the whole regex into the variable.
    my $re = do { qr/^\*.{36};(@{[join ("|", map { quotemeta } @a)]});/ }; die "not a regex!" unless ref($re) eq "Regexp"; ... $chosen_C !=~ $re ...
    Thanks to calin for the "@{[statement]}" trick.
    A reply falls below the community's threshold of quality. You may see it by logging in.