5% assure discount from here only

Monday, January 24, 2011

(2) How do i get the last element of array?

Solution:

my @array = (1,2,3,45,12,232,23);

print $array[$#array];

3 comments:

  1. my @array=(1,2,3)
    $x=pop @array
    print $x

    ReplyDelete
  2. Thnx for your comment, but pop is to remove the last element of array. No doubt $x contain the last element but it will also remove the last element from the array.

    ReplyDelete
  3. Very simple way..

    my @array = (1,2,3);
    print $array[-1]

    ReplyDelete