Re: while loop -> map



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
.



Relevant Pages

  • Re: trapping file i/o error
    ... BM> scalaror unary + and list context with parentheses. ... >> list context is not forced with parens. ... Useless use of a variable in void context at -e line 1. ...
    (comp.lang.perl.misc)
  • scalar context vs. list context (was Re: In search of Perl tutorials.)
    ... The concept of scalar context vs. list context is not mystic to anyone ... who understands a natural language. ...
    (comp.lang.perl.misc)
  • Re: How to ignore 1st line in a file when reading
    ... Jim Gibson wrote: ... However, on a line by itself, is not in list context. ... scalar context and thus just pulls one line, just like when it's in the ...
    (comp.lang.perl.misc)
  • Re[2]: regexp /g question
    ... In scalar context g causes the regex to match once per call moving the ... failed match normally resets the search position to the begin‐ ... I assumed that scalarforces a list context for its params. ...
    (perl.beginners)
  • Re: sub returning nothing
    ... Because isokisn't enforcing any calling context on its arguments, ... function), and nothingness in list context is, and empty list. ... Was NOT head of Gestapo AT ALL!" ...
    (comp.lang.perl.misc)