matching of 1 =~ /1/

Discussion in 'Perl Misc' started by Michael Goerz, Sep 26, 2006.

  1. Hi,

    what is the difference between this excerpt from my code (where in this
    instance $visit->{$field} is set to integer 1, and $pattern was set as
    qr/1/), which doesn't work, as the debugging output shows, and the
    example code underneath it, which is supposed to do the same thing, and
    works just like I expect!

    The excerpt from my code:

    # checks if visit has to be filtered out
    sub is_filtered{
    my $self = shift;
    my $visit = shift;
    while ( my ($field, $pattern) =
    each %{ $self->{_excludepatterns} } ){

    print "$visit->{$field} =~ $pattern\n"; # DEBUG
    if ( $visit->{$field} =~ $pattern){
    print "match\n"; # DEBUG
    return 1;
    } else { print "no match\n";} # DEBUG

    }
    return 0;
    }

    Debugging output:
    [...]
    0 =~ /1/
    no match
    1 =~ /1/
    no match
    1 =~ /1/
    no match
    [...]


    The example code:

    my $hashr = {};
    my $field = 'foo';
    $hashr->{$field} = 1;
    my $pattern = qr/1/;
    if ($hashr->{$field} =~ $pattern){
    print "match\n";
    } else {
    print "no match\n";
    }


    Why is the behavior not the same???

    Thank you,
    Michael Goerz
     
    Michael Goerz, Sep 26, 2006
    #1
    1. Advertising

  2. Michael Goerz

    Xicheng Jia Guest

    Michael Goerz wrote:
    > Hi,
    >
    > what is the difference between this excerpt from my code (where in this
    > instance $visit->{$field} is set to integer 1,


    > and $pattern was set as qr/1/),


    Are you should about this?? if you print out a regex object, you should
    expect a result like

    (?-xism:ptn)

    instead of

    /ptn/

    Right?

    > which doesn't work, as the debugging output shows, and the
    > example code underneath it, which is supposed to do the same thing, and
    > works just like I expect!
    >
    > The excerpt from my code:
    >
    > # checks if visit has to be filtered out
    > sub is_filtered{
    > my $self = shift;
    > my $visit = shift;
    > while ( my ($field, $pattern) =
    > each %{ $self->{_excludepatterns} } ){
    >
    > print "$visit->{$field} =~ $pattern\n"; # DEBUG
    > if ( $visit->{$field} =~ $pattern){
    > print "match\n"; # DEBUG
    > return 1;
    > } else { print "no match\n";} # DEBUG
    >
    > }
    > return 0;
    > }
    >
    > Debugging output:
    > [...]
    > 0 =~ /1/
    > no match
    > 1 =~ /1/
    > no match
    > 1 =~ /1/
    > no match
    > [...]
    >
    >
    > The example code:
    >
    > my $hashr = {};
    > my $field = 'foo';
    > $hashr->{$field} = 1;
    > my $pattern = qr/1/;


    if you add one line here:

    print "$hashr->{$field} =~ $pattern";

    you will get a result like:

    1 =~ (?-xism:1)

    compared with the result from your real code,

    1 =~ /1/

    my question to you is: Did you define the $pattern in the hash-table as
    a regex object or just a literal string.

    Regards,
    Xicheng

    > if ($hashr->{$field} =~ $pattern){
    > print "match\n";
    > } else {
    > print "no match\n";
    > }
    >
     
    Xicheng Jia, Sep 26, 2006
    #2
    1. Advertising

  3. Xicheng Jia wrote:
    >> and $pattern was set as qr/1/),

    >
    > Are you should about this?? if you print out a regex object, you should
    > expect a result like
    >
    > (?-xism:ptn)
    >
    > instead of
    >
    > /ptn/
    >
    > Right?
    > my question to you is: Did you define the $pattern in the hash-table as
    > a regex object or just a literal string.


    Yes! Thank you for pointing that out. I made a stupid mistake and stored
    the uncompiled regex in the hash-table instead of the compiled one.

    Thanks a lot,
    Michael
     
    Michael Goerz, Sep 26, 2006
    #3
    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. Seth
    Replies:
    1
    Views:
    1,090
    Ray Andraka
    Aug 1, 2003
  2. DelphiDude
    Replies:
    3
    Views:
    1,190
  3. Eric A. Hall

    weird matching problems

    Eric A. Hall, Jul 24, 2003, in forum: Perl
    Replies:
    0
    Views:
    996
    Eric A. Hall
    Jul 24, 2003
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    262
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    267
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page