Perl (Scripting) - Print last value in an array in Perl

by
Jeremy Canfield |
Updated: November 30 2021
| Perl (Scripting) articles
Let's say you have an array of fruit.
my @fruit = qw(apple banana orange grapes);
The following will print the last value in the array (grapes in this example).
print $fruit[$#fruit];
Likewise, you could store the last value in a new variable.
my $last_value = $fruit[$#fruit];
Did you find this article helpful?
If so, consider buying me a coffee over at