Indexing from the end of an array:
$last_element = $array[$#array];
Usually it's simpler and clearer to use negative subscripts:
$last_element = $array[-1];