On May 24, 2005, at 19:22, Robert Citek wrote:

I found a variation of this in the Perl Nutshell book:

$ perl -le '
 $foo="fee fie foe foo" ;
 while ($foo =~ m/e/g ) {
   push @bar, pos $foo ;
 }
 print join(":", @bar); '
2:3:7:11

Is there an equivalent way to do the same using map instead of an explicit while loop? I'm guessing not, since map is expecting a list and not a scalar, which $foo is.

The difficulty comes from the need to figure out the indices, a possible approach would be:

    my $i = 0;
    my @bar = map $_->[1],           # take second component
              grep $_->[0] eq 'e',   # let 'e's pass
              map [$_, ++$i],        # arrayref [char, index of char]
              split //, $foo;        # split $foo in chars

The while is better IMO.

-- fxn

--
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