Pattern matching : not matching problem

Discussion in 'Perl Misc' started by Marc Bissonnette, Jan 8, 2004.

  1. Hi all;

    The answer to this is probably simple, but I can't figure it out :(

    I want to test a string for a value in a content-checking subroutine: If
    there is a match (if it meets criteria), then it is to continue on with
    the rest of the sub. If it fails the match, then I want to display an
    error.

    I (obviously) have not understood the 'not match' rules or usage:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my $string = "foobar";
    my $matchstring = 'foo';

    if ($string ne~ /$matchstring/) {
    print " No Match\n";
    } else {
    print " Match\n";
    }

    Results in " Match" no matter what is in $matchstring or $string.
    The interpreter also replies with

    "Use of uninitialized value in pattern match (m//) at c:\test.pl line 7"

    Which I thought was supposed to be the purpose behind declaring the vars
    with my up front...

    I've tried
    if ($string ne /$matchstring/)

    and

    if ($string !=~ /$matchstring/)

    even though != is supposed to be numerics.

    I know I can test for a positive match (ie.
    if $string =~ /$matchstring/) {
    #do something;
    } else {
    #do something else;
    }

    But I'm curious as to why I can't do the "If this doesn't match do this"
    type of approach.

    I would be most appreciative for any insight you could provide.

    --
    Marc Bissonnette
    CGI / Database / Web Management Tools: http://www.internalysis.com
    Something To Sell? Looking To Buy? http://www.whitewaterclassifieds.ca
    Looking for a new ISP? http://www.canadianisp.com
     
    Marc Bissonnette, Jan 8, 2004
    #1
    1. Advertising

  2. Marc Bissonnette

    gnari Guest

    "Marc Bissonnette" <> wrote in message
    news:Xns946A93E338B99dragnetinternalysisc@207.35.177.135...
    > Hi all;
    >
    > The answer to this is probably simple, but I can't figure it out :(
    >
    > if ($string ne~ /$matchstring/) {
    > print " No Match\n";
    > } else {
    > print " Match\n";
    > }
    >


    you want to use
    $string !~ /$matchstring/

    gnari
     
    gnari, Jan 8, 2004
    #2
    1. Advertising

  3. Marc Bissonnette <> wrote in
    news:Xns946A93E338B99dragnetinternalysisc@207.35.177.135:

    > I want to test a string for a value in a content-checking subroutine:
    > If there is a match (if it meets criteria), then it is to continue on
    > with the rest of the sub. If it fails the match, then I want to
    > display an error.
    >
    > I (obviously) have not understood the 'not match' rules or usage:


    perldoc perlop
    perldoc perlre

    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > my $string = "foobar";
    > my $matchstring = 'foo';
    >
    > if ($string ne~ /$matchstring/) {


    What do you actually want to test?

    (A) The contents of $string are not the same as the contents of
    $matchstring

    or

    (B) The contents of $matchstring do not occur anywhere in $string

    You can check each by using the appropriate operator instead of combining
    various different ones:

    A: if($string ne $matchstring)

    B: if($string !~ /$matchstring/)

    ....

    > I've tried
    > if ($string ne /$matchstring/)
    >
    > and
    >
    > if ($string !=~ /$matchstring/)
    >
    > even though != is supposed to be numerics.


    Well, it seems like the only thing you haven't tried is reading the
    documents.

    From perldoc perlre:

    Binary "!~" is just like "=~" except the return value is negated in the
    logical sense.

    ....

    Binary "ne" returns true if the left argument is stringwise not equal to
    the right argument.

    I cannot find any mention of !=~ and I am baffled by your approach to
    computer programming.

    > But I'm curious as to why I can't do the "If this doesn't match do
    > this" type of approach.


    It does work:

    C:\Home> cat ttt.pl
    #! perl

    use strict;
    use warnings;

    my $string = "foobar";
    my $matchstring = 'foo';

    print '$string is stringwise ';

    if($string ne $matchstring) {
    print 'not equal to ';
    } else {
    print 'equal to ';
    }

    print '$matchstring', "\n";

    print '$matchstring ';

    if($string !~ /$matchstring/) {
    print 'does not occur ';
    } else {
    print 'occurs ';
    }

    print 'in $string', "\n";

    __END__



    C:\Home> perl ttt.pl
    $string is stringwise not equal to $matchstring
    $matchstring occurs in $string
     
    A. Sinan Unur, Jan 8, 2004
    #3
  4. "gnari" <> wrote in news:btkcpj$nbj$:

    >
    > "Marc Bissonnette" <> wrote in message
    > news:Xns946A93E338B99dragnetinternalysisc@207.35.177.135...
    >> Hi all;
    >>
    >> The answer to this is probably simple, but I can't figure it out :(
    >>
    >> if ($string ne~ /$matchstring/) {
    >> print " No Match\n";
    >> } else {
    >> print " Match\n";
    >> }
    >>

    >
    > you want to use
    > $string !~ /$matchstring/


    AH! Thank you!!!

    I got it muddled in my mind and just couldn't figure the right terms to
    search on via Google to find the answer (and I looked - asking questions in
    this forum is often an excercise in asbestos tolerance)

    Thank you, again!



    --
    Marc Bissonnette
    CGI / Database / Web Management Tools: http://www.internalysis.com
    Something To Sell? Looking To Buy? http://www.whitewaterclassifieds.ca
    Looking for a new ISP? http://www.canadianisp.com
     
    Marc Bissonnette, Jan 8, 2004
    #4
  5. "A. Sinan Unur" <> wrote in
    news:Xns946A996CE36A1asu1cornelledu@132.236.56.8:

    [snip]

    > Well, it seems like the only thing you haven't tried is reading the
    > documents.


    Indeed. I missed it, I messed up and came seeking help. Thank you for
    providing it.

    > From perldoc perlre:
    >
    > Binary "!~" is just like "=~" except the return value is negated in
    > the logical sense.
    >
    > ...
    >
    > Binary "ne" returns true if the left argument is stringwise not equal
    > to the right argument.
    >
    > I cannot find any mention of !=~ and I am baffled by your approach to
    > computer programming.


    Alas, some of us make mistakes in an ongoing learning process. I can only
    aspire to never make a silly mistake again.

    >> But I'm curious as to why I can't do the "If this doesn't match do
    >> this" type of approach.

    >
    > It does work:


    Indeed - when you do it correctly, as opposed to incorrectly, which was
    my case and my code.

    Thanks for the information and the time you took with your examples.

    [snip]


    --
    Marc Bissonnette
    CGI / Database / Web Management Tools: http://www.internalysis.com
    Something To Sell? Looking To Buy? http://www.whitewaterclassifieds.ca
    Looking for a new ISP? http://www.canadianisp.com
     
    Marc Bissonnette, Jan 8, 2004
    #5
  6. Marc Bissonnette <> wrote in message news:<Xns946A93E338B99dragnetinternalysisc@207.35.177.135>...
    > Hi all;
    >
    > The answer to this is probably simple, but I can't figure it out :(


    You guessed right :)

    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > my $string = "foobar";
    > my $matchstring = 'foo';
    >
    > if ($string ne~ /$matchstring/) {

    ^^
    That should be:
    if ($string !~ /$matchstring/) {

    > print " No Match\n";
    > } else {
    > print " Match\n";
    > }
    >


    You might want to use index for your job. It will probably run faster
    than evaluating the regular expression and has simple syntax.

    my $idx = index($string, $matchstring);
    if ($idx != -1) {
    print "Found $matchstring at pos $idx\n";
    }
    else {
    print "No match\n";
    }

    Hope this helps,
    Chris
     
    Chris Charley, Jan 9, 2004
    #6
  7. (Chris Charley) wrote in
    news::

    > Marc Bissonnette <> wrote in message
    > news:<Xns946A93E338B99dragnetinternalysisc@207.35.177.135>...
    >> Hi all;
    >>
    >> The answer to this is probably simple, but I can't figure it out :(

    >
    > You guessed right :)
    >
    >> #!/usr/bin/perl
    >> use strict;
    >> use warnings;
    >> my $string = "foobar";
    >> my $matchstring = 'foo';
    >>
    >> if ($string ne~ /$matchstring/) {

    > ^^
    > That should be:
    > if ($string !~ /$matchstring/) {
    >
    >> print " No Match\n";
    >> } else {
    >> print " Match\n";
    >> }
    >>

    >
    > You might want to use index for your job. It will probably run faster
    > than evaluating the regular expression and has simple syntax.
    >
    > my $idx = index($string, $matchstring);
    > if ($idx != -1) {
    > print "Found $matchstring at pos $idx\n";
    > }
    > else {
    > print "No match\n";
    > }


    Very much, thank you !
    - Next step for me -
    perldoc -f index

    :)



    --
    Marc Bissonnette
    CGI / Database / Web Management Tools: http://www.internalysis.com
    Something To Sell? Looking To Buy? http://www.whitewaterclassifieds.ca
    Looking for a new ISP? http://www.canadianisp.com
     
    Marc Bissonnette, Jan 9, 2004
    #7
  8. Marc Bissonnette

    Robin Guest

    the opererator your looking for is !~ instead of ne~ -

    --
    Regards,
    Robin
    --

    --

    "Marc Bissonnette" <> wrote in message
    news:Xns946A93E338B99dragnetinternalysisc@207.35.177.135...
    > Hi all;
    >
    > The answer to this is probably simple, but I can't figure it out :(
    >
    > I want to test a string for a value in a content-checking subroutine: If
    > there is a match (if it meets criteria), then it is to continue on with
    > the rest of the sub. If it fails the match, then I want to display an
    > error.
    >
    > I (obviously) have not understood the 'not match' rules or usage:
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > my $string = "foobar";
    > my $matchstring = 'foo';
    >
    > if ($string ne~ /$matchstring/) {
    > print " No Match\n";
    > } else {
    > print " Match\n";
    > }
    >
    > Results in " Match" no matter what is in $matchstring or $string.
    > The interpreter also replies with
    >
    > "Use of uninitialized value in pattern match (m//) at c:\test.pl line 7"
    >
    > Which I thought was supposed to be the purpose behind declaring the vars
    > with my up front...
    >
    > I've tried
    > if ($string ne /$matchstring/)
    >
    > and
    >
    > if ($string !=~ /$matchstring/)
    >
    > even though != is supposed to be numerics.
    >
    > I know I can test for a positive match (ie.
    > if $string =~ /$matchstring/) {
    > #do something;
    > } else {
    > #do something else;
    > }
    >
    > But I'm curious as to why I can't do the "If this doesn't match do this"
    > type of approach.
    >
    > I would be most appreciative for any insight you could provide.
    >
    > --
    > Marc Bissonnette
    > CGI / Database / Web Management Tools: http://www.internalysis.com
    > Something To Sell? Looking To Buy? http://www.whitewaterclassifieds.ca
    > Looking for a new ISP? http://www.canadianisp.com
     
    Robin, Jan 13, 2004
    #8
  9. Marc Bissonnette

    Robin Guest

    Sorry 'bout that top post, I just pressed the send button a little too
    quick.
     
    Robin, Jan 13, 2004
    #9
  10. Marc Bissonnette, Jan 13, 2004
    #10
    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. anshul
    Replies:
    2
    Views:
    1,840
  2. Ari Brown

    Pattern Matching Problem

    Ari Brown, Jul 6, 2007, in forum: Ruby
    Replies:
    12
    Views:
    197
    Morton Goldberg
    Jul 6, 2007
  3. Bryan

    Pattern matching problem

    Bryan, Jun 12, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    132
  4. rader
    Replies:
    4
    Views:
    202
    Ernest Lergon
    May 7, 2005
  5. Bobby Chamness
    Replies:
    2
    Views:
    266
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page