For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > Stubborn regex :-(









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Stubborn regex :-(
Frostillicus

2004-10-28, 8:56 am

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.


Arndt Jonasson

2004-10-28, 8:56 am


"Frostillicus" <frosty@nilspamos.iinet.net.au> writes:
> 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!";
}
Frostillicus

2004-10-28, 8:56 am

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




"Frostillicus" <frosty@nilspamos.iinet.net.au> wrote in message
news:4180a689$0$13746$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
> 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.
>
>



Paul Lalli

2004-10-28, 8:57 am

Frostillicus wrote:
> "Frostillicus" <frosty@nilspamos.iinet.net.au> wrote in message
> news:4180a689$0$13746$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com