Feed on

to get the last element of an array in perl, use the length of the array as your index.

for instance, if my array is very creatively named “my_array,”

the length of the array is:


and it’s last element is


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

3 Responses to “get the last element of an array in perl”

  1. on 04 Oct 2009 at 3:44 am Sub#

    $my_array[-1] would me more easier …

  2. on 14 Sep 2011 at 8:11 am Rupesh

    thats the prefect trick man.!!!

  3. on 28 Aug 2014 at 5:26 am ebioman

    “$my_array[-1] would me more easier …”

    I would say that both serve a different need, even though they both get the same value.

    I believe it is more save with the $#my_array in case you have an array with 1 or 0 entries

Did I get this wrong? Let me know!

Trackback URI | Comments RSS