Stubborn regex :-(

F

Frostillicus

I'm having trouble getting a regex to evaluate to true, even though it looks
plainly obvious to me that it should be. Here's what I'm doing...

print "find=" . $form{'find'} . ";name=" . $hashref->{'name'};


This line of code prints the following for an example set of data...

find=ant;name=The Ant's Pants.


Here's the code...

my $temp = $hashref->{'name'};

if ($form{'find'} =~ /$temp/i) {
print "Found record!";
}



You might say that I shouldn't need to stuff the hashref into a $temp
variable, but I did that in desperation in case some bizarre problem has
preventing me from matching on a value inside a reference to a hash.
 
A

Arndt Jonasson

Frostillicus said:
I'm having trouble getting a regex to evaluate to true, even though it looks
plainly obvious to me that it should be. Here's what I'm doing...

print "find=" . $form{'find'} . ";name=" . $hashref->{'name'};


This line of code prints the following for an example set of data...

find=ant;name=The Ant's Pants.


Here's the code...

my $temp = $hashref->{'name'};

if ($form{'find'} =~ /$temp/i) {
print "Found record!";
}

I think you want the operands of =~ the other way around. What the
above amounts to is

if ("ant" =~ /The Ant's Pants/i) {
print "Found record!";
}
 
P

Paul Lalli

Frostillicus said:
Hmm, my bad... If I swap the variables around, voilas! Grrrrr!

For future reference, it is sometimes helpful to read the =~ as
"contains" rather than "matches". If you do that, your original
statement reads:
if 'ant' contains "The Ant's Pants" then print 'Found Record!'.

If you read it allowed like this, it becomes obvious that the
'containment' is reversed.

Paul Lalli
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top