How do I pattern match variables in Perl?

G

Graham

I want to match $variable in an array. My script is as follows:-

foreach ($line(@lines)
{
if ($line =~ /\$variable/i)
{
$counter++;
last;
}
}

The array definitely contains $variable, but I get no match. I've tried
countless variations of the line:
if ($line =~ /\$variable/i)
all to noavail
Can someone put me out of my misery, please?
 
P

Peter Wyzl

:I want to match $variable in an array. My script is as follows:-
:
: foreach ($line(@lines)
: {
: if ($line =~ /\$variable/i)
: {
: $counter++;
: last;
: }
: }
:
: The array definitely contains $variable, but I get no match. I've tried
: countless variations of the line:
: if ($line =~ /\$variable/i)
: all to noavail
: Can someone put me out of my misery, please?


foreach $line (@lines){
if ($line =~ /$variable/i){
$counter++;
last;
}
}


Removed two characters and re-formatted for easier reading... can you spot
the difference?

*hint - Why escape the variable??

P
 
J

Joe Smith

Graham said:
I want to match $variable in an array. My script is as follows:-
The array definitely contains $variable, but I get no match.

Are you trying to match "dollar-sign vee ay ar eye ay bee el ee"
or are you trying to match the string held in the variable $variable
or are you trying to match a regex which is stored in $variable?

$line =~ /$variable/; # $variable holds a regex
$line =~ /\$variable\b/; # Looking for dollar-sign vee ...
$line =~ /\Q$variable/; # $variable hold text with punctuation
Can someone put me out of my misery, please?

First you have to tell us exactly what you're trying to do.
It sounds like you yourself are not sure what you want.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top