On 5/24/05, Robert Citek <[EMAIL PROTECTED]> wrote:
> 
> On May 24, 2005, at 3:14 PM, Jay Savage wrote:
> > One thing that springs to mind is:
> >
> >    perl -le '
> >    $foo = "fee fie foe foo";
> >    map {$i++; push @bar, $i if $_ eq "e"} split //, $foo;
> >    print join(":",@bar)'
> >
> > I'm not sure if it gains you anything, though: as you say, map is
> > designed for lists.
> 
> I like your idea.  Unfortunately, the above works only in the special
> case where the regular expression match is actually a single-
> character, exact match.
> 
> Regards,
> - Robert

It works for your example case; if you have a different case in mind,
there will be a different solution.  There is rarely a "general case"
in perl, especially if the goal is to keep the code a reasonable
one-liner invoked with 'perl -le'.  constructions using map in
particular yeild very specific results.

If you want to avoid while without using map you can also do something
like the following:

  $_ = $foo; s/e/push @bar, pos/eg;

Make sure to copy the variable, though; s/// will mangle it.  Note, too, that 

  /e(?{push @bar, pos})/g;

should work, but seems to ignore the /g.

HTH,

--jay
--------------------
daggerquill [at] gmail [dot] com
http://www.engatiki.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to