On 5/24/05, 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.
> 

This is Perl, these is always another way :)

my $foo = "fee fie foe foo"; 
my $sum;
my @bar = map {$sum+=1+length} split(/e/,$foo);
print join(":", @bar[0..$#bar-1]) . "\n";

It is however a bit forced. I would stick to the "while" - much more readable :)
Cheers,
-- 
Offer Kaye

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