Pattern matching : not matching problem

  • Thread starter Marc Bissonnette
  • Start date
M

Marc Bissonnette

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.
 
G

gnari

Marc Bissonnette said:
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
 
A

A. Sinan Unur

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
 
M

Marc Bissonnette

gnari said:
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!
 
M

Marc Bissonnette

[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.
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]
 
C

Chris Charley

Marc Bissonnette said:
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
 
M

Marc Bissonnette

(e-mail address removed) (Chris Charley) wrote in
You guessed right :)

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


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

:)
 
M

Marc Bissonnette

Robin said:
Sorry 'bout that top post, I just pressed the send button a little too
quick.

Get a real newsreader and you'll avoid having to correct Microsoft's
errors.

I would suggest Xhttp://xnews.newsguy.com/
 

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

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top