35

If we have array then we can do following:

my @arr = qw(Field3 Field1 Field2 Field5 Field4);
my $last_arr_index=$#arr;

How do we do this for array reference?

my $arr_ref = [qw(Field3 Field1 Field2 Field5 Field4)];
my $last_aref_index; # how do we do something similar to $#arr;

4 Answers 4

60
my $arr_ref = [qw(Field3 Field1 Field2 Field5 Field4)];
my ($last_arr_index, $next_arr_index);

If you need to know the actual index of last element, for example you need to loop over the array's elements knowing the index, use $#$:

$last_arr_index = $#{ $arr_ref };
$last_arr_index = $#$arr_ref; # No need for {} for single identifier

If you need to know the index of element after the last, (e.g. to populate next free element without push()),

OR you need to know the amount of elements in the array (which is the same number) as above:

my $next_arr_index = scalar(@$arr_ref);
$last_arr_index = $next_arr_index - 1; # in case you ALSO need $last_arr_index
# You can also bypass $next_arr_index and use scalar, 
# but that's less efficient than $#$ due to needing to do "-1"
$last_arr_index = @{ $arr_ref } - 1; # or just "@$arr_ref - 1"
   # scalar() is not needed because "-" operator imposes scalar context 
   # but I personally find using "scalar" a bit more readable
   # Like before, {} around expression is not needed for single identifier

If what you actually need is to access the last element in the arrayref (e.g. the only reason you wish to know the index is to later use that index to access the element), you can simply use the fact that "-1" index refers to the last element of the array. Props to Zaid's post for the idea.

$arr_ref->[-1] = 11;
print "Last Value : $arr_ref->[-1] \n";
# BTW, this works for any negative offset, not just "-1". 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks DVK. That's exactly what I was thinking when I penned down my answer.
18
my $last_aref_index = $#{ $arr_ref };

1 Comment

Can't tell you how many times I've used this answer (and looked it up again, since I keep forgetting the exact syntax). A classic! Short & sweet (and correct).
9

The reason you probably need to access the last index is to get the last value in the array reference.

If that is the case, you can simply do the following:

$arr_ref->[-1];

The -> operator dereferences. [-1] is the last element of the array.

If you need to count the number of elements in the array, there's no need to do $#{ $arr_ref } + 1. DVK has shown a couple of better ways to do it.

5 Comments

But that gives you the value of the last element, not its index.
Yes, but why else would one want the last index? I've qualified my answer accordingly.
We may require it my $hash_ref = {map { $arr_ref->[$_] => $_ } 0..$#{ $arr_ref }};
you're right (generically, he MIGHT only need to access last element without knowing last index), but specifically, knowing that index is needed for, as Sachin's comment illustrated, looping over array's indices (instead of array's elements). +1 for thinking of possible X-Y problem
@Sachin : Point well taken. I rest my case.
0
my $arr_ref = [qw(Field3 Field1 Field2 Field5 Field4)];

my $last_aref_index = $$arr_ref[$#{$arr_ref}];
print "last element is: $last_aref_index\n";

2 Comments

in the future, you may want to use the code formatting (ident by 4 spaces or click "code" button in editor)
thanks. I may also want to read the question more carefully, as my example returns "Field4" as opposed to the index #...jw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.