SPUG: last index array reference's referent

veritosproject at gmail.com veritosproject at gmail.com
Sun Jan 21 15:43:47 PST 2007


I tend to use a for loop for stuff like this:

On 1/21/07, Doug Treder <dtreder at gmail.com> wrote:
> I've never thought of $#array as elegant at all, and the fact that my
> left fingers curl into a painful knot to type it as Larry's way of
> telling me so.  Also note that indexing into an array is much slower
> than aliasing it in a foreach loop.  So my favorite idiom for this is:
>
> my $i = 0;
> foreach my $item (@$array_ref) {
>    print $i++ .' ' . $item . "\n";
> }

print $i . ' ' . $array_ref->[$i] . "\n" for (my $i = 0; $i <=
$#{$array_ref}; ++$i);


More information about the spug-list mailing list