in reply to use simple scalar names to point to specific array elements

I don't think anybody has mentioned this here, so I'll tell you now that it's possible to create an alias to a scalar using a for/foreach loop. Of course, it will ever loop only once, so it's more or less how one could write a with statement in Perl.

This will do what you ask for:

for my $fifthele($myarray[5]) { # inside this loop body, $fifthele is an alias to $myarray[5] $fifthele = 123; }

I wish Perl had a more appropriate keyword, then people would feel more at ease using this idiom, but let me assure you: it works fine, and it's 100% reliable.

Replies are listed 'Best First'.
Re: Re: use simple scalar names to point to specific array elements
by ambrus (Abbot) on Dec 08, 2003 at 19:08 UTC

    Foreach is a good solution as long as you don't assign the array as a whole like @myarray= map {$_, $_+1} @myarray;.

    I don't exactly know what the consequences of that are, but it's illegal, and perl can't catch it. update: Removed comma after map{whatever}