in reply to Modify array elements inside for loops

my @new_a = map { $_+1 } @a;
(faster? maybe not, but cooler and shorter, yes =)

Whether or not this is what you want depends on what you are planning to do with the modified values. You could always do:

foreach ( map { $_ +1 } @a ) { #whatever }

As well. HTH

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
Re: Re: Modify array elements inside for loops
by merlyn (Sage) on Mar 06, 2001 at 21:40 UTC
    Hmm. After experimenting (albeit with 5.5.3, the most recent usable release), I have seen that:
    foreach (map $_, @a) { $_++; # then whatever }
    is sufficient. Cool, I'll add that to my trick bag. Much easier than that crazy nested @{[@a]} I've seen so often (and think I actually invented from first principles at one point).

    -- Randal L. Schwartz, Perl hacker