Stubborn regex :-(

Discussion in 'Perl Misc' started by Frostillicus, Oct 28, 2004.

  1. Frostillicus

    Frostillicus Guest

    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.
     
    Frostillicus, Oct 28, 2004
    #1
    1. Advertising

  2. Frostillicus

    Frostillicus Guest

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




    "Frostillicus" <> wrote in message
    news:4180a689$0$13746$...
    > 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.
    >
    >
     
    Frostillicus, Oct 28, 2004
    #2
    1. Advertising

  3. "Frostillicus" <> 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!";
    }
     
    Arndt Jonasson, Oct 28, 2004
    #3
  4. Frostillicus

    Paul Lalli Guest

    Frostillicus wrote:
    > "Frostillicus" <> wrote in message
    > news:4180a689$0$13746$...
    >
    >>
    >>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.
    >>

    > 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
     
    Paul Lalli, Oct 28, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    20
    Views:
    903
  2. Why is C compiler stubborn ?

    , Mar 13, 2007, in forum: C Programming
    Replies:
    45
    Views:
    979
    Chris Dollin
    Mar 19, 2007
  3. Replies:
    3
    Views:
    808
    Reedick, Andrew
    Jul 1, 2008
  4. Wybo Dekker

    stubborn program using readline

    Wybo Dekker, Jun 16, 2005, in forum: Ruby
    Replies:
    8
    Views:
    184
    Ara.T.Howard
    Jun 17, 2005
  5. tip perday
    Replies:
    0
    Views:
    91
    tip perday
    Jun 22, 2009
Loading...

Share This Page