Monday, January 3, 2011

Perl: 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.

No comments: