Perl Style: Negative Array Subscripts

  • To get the last element in a list or array, use $array[-1] instead of $array[$#array]. The former works on both lists and arrays, but the latter does not.

  • Remember that substr and splice also accept negative subscripts to count back from the end.

        split(@array, -2);   # pop twice
    
  • Remember substr is lvaluable:

        substr($s, -10) =~ s/ /./g;
    

Forward to Embrace Hashes
Back to Changing en passant
Up to index

Copyright © 1998, Tom Christiansen All rights reserved.

Tags

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub

TPRF Gold Sponsor
TPRF Silver Sponsor
TPRF Bronze Sponsor