Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Resetting //g

Reply
Thread Tools

Resetting //g

 
 
Roy Johnson
Guest
Posts: n/a
 
      10-29-2003
If you short-circuit out of a global pattern match like so:
for (1..$n) {
$str =~ /($pat)/g;
$NthMatch = $1;
}
where there are more than $n matches, the next time you do
$str =~ /($pat)/g;
even if it's in a completely different block of code, the matching is
going to pick up where it left off. Is there a way to reset it, short
of whiling away the rest of the matches? (I tried several arguments
for the reset function.)

Incidentally, the best way to get the $nth match of $pat in $str is
$str =~ /(?:.*?($pat)){$n}/;
but I'm still curious about short-circuited global matches.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-29-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Roy Johnson) wrote:
> If you short-circuit out of a global pattern match like so:
> for (1..$n) {
> $str =~ /($pat)/g;
> $NthMatch = $1;
> }
> where there are more than $n matches, the next time you do
> $str =~ /($pat)/g;
> even if it's in a completely different block of code, the matching is
> going to pick up where it left off. Is there a way to reset it, short
> of whiling away the rest of the matches? (I tried several arguments
> for the reset function.)


From perldoc perlop:

| The position after the last match can be read or set using the pos()
| function; see "pos" in perlfunc.

/Nota bene/ that you call pos on $str, not $pat.

> Incidentally, the best way to get the $nth match of $pat in $str is
> $str =~ /(?:.*?($pat)){$n}/;
> but I'm still curious about short-circuited global matches.


I would have said a better way would be
$nthmatch = ($str =~ /($pat)/g)[$n];
, not least because it actually works, but maybe that's just me...

Ben

--
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
-Assyrian stone tablet, c.2800 BC (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      10-29-2003
[posted & mailed]

On 29 Oct 2003, Roy Johnson wrote:

>If you short-circuit out of a global pattern match like so:
> for (1..$n) {
> $str =~ /($pat)/g;
> $NthMatch = $1;
> }
>where there are more than $n matches, the next time you do
> $str =~ /($pat)/g;
>even if it's in a completely different block of code, the matching is
>going to pick up where it left off. Is there a way to reset it, short
>of whiling away the rest of the matches? (I tried several arguments
>for the reset function.)


You can set pos($str) to undef.

for (1 .. $n) {
$str =~ /($pat)/g;
$last = $1;
}
undef pos($str);

The /g flag makes the regex start looking at pos($str) next time; setting
it to undef makes it start looking at the beginning of the string again.

--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
Roy Johnson
Guest
Posts: n/a
 
      10-29-2003
Ben Morrow <(E-Mail Removed)> wrote in message news:<bnoo6h$hr4$(E-Mail Removed)>...
> From perldoc perlop:
>
> | The position after the last match can be read or set using the pos()
> | function; see "pos" in perlfunc.


I shoulda thought of that. Thanks.

> /Nota bene/ that you call pos on $str, not $pat.
>
> > Incidentally, the best way to get the $nth match of $pat in $str is
> > $str =~ /(?:.*?($pat)){$n}/;
> > but I'm still curious about short-circuited global matches.

>
> I would have said a better way would be
> $nthmatch = ($str =~ /($pat)/g)[$n];
> , not least because it actually works, but maybe that's just me...


What makes you think that my pattern doesn't work? It does, while
yours actually doesn't: you need to index $n-1 unless you've reset $[
to 1. The difference is that yours does about twice the work, and so
takes about twice as long. The aborting for loop is even slower.

Some benchmark code for your amusement:

#!perl

use strict;
use warnings;
use Benchmark;

my $str='abcabbcabbbbcabcabbcab';
my $n = 3; ## Find the $nth occurrence
my $pat = qr/ab+/; ## of this pattern

sub pat_n;
sub for_g;
sub m_g;

print "pat_n Match $n in $str is ", pat_n, "\n";
print "for_g Match $n in $str is ", for_g, "\n";
print "m_g Match $n in $str is ", m_g, "\n";

timethese( 100_000, {
'$pat{$n}' => \&pat_n,
'for //g' => \&for_g,
'm_g' => \&m_g,
});

sub pat_n {
$str =~ /(?:.*?($pat)){$n}/;
}

sub for_g {
my $NthMatch;
for (1..$n) {
$str =~ /($pat)/g;
$NthMatch = $1;
}
pos($str) = 0;
$NthMatch;
}

sub m_g {
($str =~ /($pat)/g)[$n-1];
}
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-29-2003

(E-Mail Removed) (Roy Johnson) wrote:
> Ben Morrow <(E-Mail Removed)> wrote in message
> news:<bnoo6h$hr4$(E-Mail Removed)>...
> > > Incidentally, the best way to get the $nth match of $pat in $str is
> > > $str =~ /(?:.*?($pat)){$n}/;
> > > but I'm still curious about short-circuited global matches.

> >
> > I would have said a better way would be
> > $nthmatch = ($str =~ /($pat)/g)[$n];
> > , not least because it actually works, but maybe that's just me...

>
> What makes you think that my pattern doesn't work?


Sorry, I must have misread it... or something. I thought it would fail
on inputs like
ab ab ab abb
and get the 'abb' instead of the third 'ab', but I was wrong.

> It does, while yours actually doesn't: you need to index $n-1 unless
> you've reset $[ to 1.


Yes, of course...

> The difference is that yours does about twice the work, and so
> takes about twice as long. The aborting for loop is even slower.
>
> Some benchmark code for your amusement:


Actually, on my machine my code runs slowest of the three for that
input...

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
(E-Mail Removed) <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resetting wireless setup =?Utf-8?B?SmltNzg5?= Wireless Networking 2 10-31-2005 03:39 AM
printer resetting default for networked computers =?Utf-8?B?Q29ubmll?= Wireless Networking 1 07-27-2005 01:40 PM
gateway not resetting Dwight Wireless Networking 2 04-15-2005 07:52 PM
Resetting =?Utf-8?B?U25vb2s=?= Wireless Networking 4 02-03-2005 08:18 AM
Resetting icon in upper right. slacK_ Firefox 2 07-16-2004 06:29 AM



Advertisments