in reply to Hidden Secrets of PERL

@a=(1,2,3); foreach my $r (@a) { $r=2; } print @a;
Have a fun ;-)

Replies are listed 'Best First'.
Re^2: Hidden Secrets of PERL
by rinceWind (Monsignor) on Oct 13, 2006 at 16:22 UTC

    What you are experiencing is that for(each) loops alias the iterator variable to each member of the list in turn. This can be written to, as this scriptlet demonstrates.

    From perldoc perlsyn:

    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)