To get the length of an array in Perl, you can use the scalar
function.
In this example,
@numbers
with some values.scalar
function to get the length of the array and store the returned value in $length
scalar variable.$length
to standard output.@numbers = (1, 2, 3, 4, 5);
$length = scalar(@numbers);
print "Length of array is: $length\n";
Length of array is: 5
In this example,
@names
with some strings.scalar
function to get the length of the array and store the returned value in $length
scalar variable.$length
to standard output.@names = ("Alice", "Bob", "Charlie");
$length = scalar(@names);
print "Length of array is: $length\n";
Length of array is: 3
In this tutorial, we learned How to Get Array Length in Perl language with well detailed examples.