9.2. Negative Indexes

The expression $myarray[-$n] is equivalent to $myarray[scalar(@myarray)-$n]. I.e: subscripts with negative indexes return the $n'th element from the end of the array. So to get the value of the last element you can write $myarray[-1] and for the second last $myarray[-2], etc.

Note that one should also make sure that array subscripts that are continuously decremented will not underflow below 0, or else one will start getting the elements from the end of the array.


Written by Shlomi Fish