Re: while loop -> map
- From: daggerquill@xxxxxxxxx (Jay Savage)
- Date: Wed, 25 May 2005 14:32:51 -0400
On 5/25/05, Jeff 'japhy' Pinyan <japhy@xxxxxxxxxxxx> wrote:
> On May 25, Jay Savage said:
>
> > /e(?{push @bar, pos})/g;
> >
> > should work, but seems to ignore the /g.
>
> Because as you wrote it, the regex is in void context, which means it'll
> only match once. Put it in list context:
>
> () = /e(?{ push @bar, pos })/g;
>
> But this looks weird to almost anyone. I'd do:
>
> /e(?{ push @bar, pos })(?!)/;
Thanks. this makes sense. But why does the zero-width lookahead
force list context? And why does /g by itself force list context for
s/// with the same search string?
>
> If 'e' is more than one character, you'll need to use
>
> /(?>pattern)(?{ push @bar, pos })(?!)/;
Assuming that the patter to matc on = "pattern", this works, too:
/p(?:{push @bar, pos})attern(?!)/g
>
> You could also use $-[0] instead of pos().
>
That depends on the context. $-[0] holds the value of the beginning of
the current match. pos() returns the position at the end of the
current match, i.e. $+[0], and is what the OP used. $-[0] = pos() -
length($&), more or less. For single-character matches, this has the
effect of making values returned from $-[0] zero indexed, and values
returned from pos() one indexed. (That's also why any (?{}) expression
with pos needs to be inserted after the frist character of a
multi-character match.)
--jay
--------------------
daggerquill [at] gmail [dot] com
http://www.engatiki.org
.
- Follow-Ups:
- Re: while loop -> map
- From: Jeff 'japhy' Pinyan
- Re: while loop -> map
- From: Jay Savage
- Re: while loop -> map
- References:
- while loop -> map
- From: Robert Citek
- Re: while loop -> map
- From: Jay Savage
- Re: while loop -> map
- From: Jeff 'japhy' Pinyan
- while loop -> map
- Prev by Date: @ISA and the paths in 'use libs'
- Next by Date: Re: @ISA and the paths in 'use libs'
- Previous by thread: Re: while loop -> map
- Next by thread: Re: while loop -> map
- Index(es):
Relevant Pages
|