Strange results of "global match".

K

Krivenok Dmitry

Hello guys!

Can anyone explain me the following results:

krivenok@olimpico ~ $ cat global_match.pl
use strict;

my $str = "aaa noSuchName sadsad asdsa dsa dsad";

while(<>)
{
# NOTE: ig
if($str =~ /noSuchName/ig)
{
print "YES\n";
}
else
{
print "NO\n";
}
}
krivenok@olimpico ~ $ perl global_match.pl

YES

NO

YES

NO

YES

Thank you beforehand!
 
M

Mirco Wahab

Krivenok said:
Hello guys!

Can anyone explain me the following results:

krivenok@olimpico ~ $ cat global_match.pl
use strict;

my $str = "aaa noSuchName sadsad asdsa dsa dsad";

while(<>)
{
# NOTE: ig
if($str =~ /noSuchName/ig)

This is: /g and in scalar context (won't reset $str.pos() before NO_MORE_MATCH_AVAIL)

You probably want' to use ($str) =~ /noSuchName/ig (list context)
or $str =~ /noSuchName/i (scalar context w/out /g)

first match found, str.pos() at the end of 'noSuchName'

string end reached (no other match found), str.pos() resetted to 0

first match found, str.pos() at the end of 'noSuchName'

string end reached (no other match found), str.pos() resetted to 0

first match found, str.pos() at the end of 'noSuchName'



Regards

M.
 
P

Paul Lalli

Can anyone explain me the following results:

krivenok@olimpico ~ $ cat global_match.pl
use strict;

my $str = "aaa noSuchName sadsad asdsa dsa dsad";

while(<>)
{
# NOTE: ig
if($str =~ /noSuchName/ig)
{
print "YES\n";
}
else
{
print "NO\n";
}}

krivenok@olimpico ~ $ perl global_match.pl

YES

NO

YES

NO

YES

Thank you beforehand!

The /g modifier starts a "progressive match". That is, it will search
for the first match it can find in the string. If it finds a match,
it returns true, and then *remembers* where it found that match. The
next time the same pattern is executed, the pattern match starts
searching from the point at which it left off. If it finds another
match, it again returns true and remembers where it left off again.
And so on. When it finally cannot find any more matches, it returns
false, and resets the position pointer to the beginning of the string.

So in your example, it found noSuchName starting at position 4 in the
string. It returned true, and kept the position pointer at position
14 - immediately after the successful match. Then the next time
through your while loop, it starts searching the string at position
14, and of course does not find another match. So it returns false,
and resets the position pointer back to the beginning of the string.

The third time through, it once again finds noSuchName at position 4,
so returns true, and updates the position pointer to 14. Etc etc etc.

For more information:
perldoc -f pos
perldoc perlre

Hope this helps,
Paul Lalli
 
P

Peter Makholm

Krivenok Dmitry said:
Can anyone explain me the following results:

You might want to read what 'perldoc perlop' says about m//g in scalar
context:

In scalar context, each execution of "m//g" finds the next
match, returning true if it matches, and false if there is no
further match. The position after the last match can be read
or set using the pos() function; see "pos" in perlfunc. A
failed match normally resets the search position to the begin-
ning of the string, but you can avoid that by adding the "/c"
modifier (e.g. "m//gc"). Modifying the target string also
resets the search position.

//Makholm
 
X

xhoster

Krivenok Dmitry said:
Hello guys!

Can anyone explain me the following results:

perldoc perlop can:

In scalar context, each execution of "m//g" finds
the next match, returning true if it matches, and
false if there is no further match. The position
after the last match can be read or set using the
pos() function; see "pos" in perlfunc. A failed
match normally resets the search position to the
beginning of the string, but you can avoid that by
adding the "/c" modifier

Xho
 
M

Michele Dondi

This is: /g and in scalar context (won't reset $str.pos() before NO_MORE_MATCH_AVAIL)

6isms already?
You probably want' to use ($str) =~ /noSuchName/ig (list context)

Whoah, however simple and innocent it looks, it had never occurred to
me...


Michele
 

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
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top