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

Hello PerlMonks, I have two arrays and I would like to check whether element 1 of Array1 contains the pattern of the first element in Array2 i.e. The first element of $Array1[0] contains the pattern in $Array2[0]
if $Array1[0] =~/\$Array2\[0\]/{ print "PASS"; }
thanks for your help

Replies are listed 'Best First'.
Re: How to compare Array values??
by davorg (Chancellor) on Jul 13, 2009 at 15:54 UTC

    If you escape the special characters then you'll be matching against the literal string '$array[0]' instead of the contents of $array[0].

    if ($Array1[0] =~ /$Array2[0]/) { print "PASS"; }
    --

    See the Copyright notice on my home node.

    Perl training courses

Re: How to compare Array values??
by BioLion (Curate) on Jul 13, 2009 at 15:53 UTC

    Not tested... should work though... qr and perlre should explain.

    if $Array1[0] =~/\Q$Array2[0]\E/{ print "PASS"; }
    Just a something something...
Re: How to compare Array values??
by markkawika (Monk) on Jul 13, 2009 at 17:58 UTC

    Your code doesn't match what you say.

    Do you want to check to see if the first element of Array1 contains the literal string $Array2[0] ? Because that's what your code does.

    If, instead, you have a regex stored in $Array2[0], and you want to use that to attempt to match against the string stored in $Array1[0], you need to not quote the variable:

    if ($Array1[0] =~ m{$Array2[0]}) { print "PASS\n"; }