in reply to (digression) preferred syntax for index of last array element?
in thread Initialization of arrays
There might be an extra op in the @x-1 version, but using the latter because of this would be "premature optimization". Pick whichever one you prefer. Personally, I prefer beating around the bush and use last ($#x) if I mean last, and count (@x) if I mean count. The down side is that the reader must be familiar with both ops.
But that's assuming (deprecated) $[ is left untouched.. If $[ is 1, they're both wrong. The solution for $[ = 1 is
for my $y (1..$#x) { }
or
for my $y (1..@x) { }
and the general solution is
for my $y ($[..$#x) { }
or
for my $y ($[..$[+@x-1) { }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: (digression) preferred syntax for index of last array element?
by ambrus (Abbot) on Sep 23, 2005 at 16:24 UTC |