Updated July 3, 2023
Definition of Perl array length
The return type for the Perl array length will be the number of elements that are present in the defined array with a convention that it will always return the physical size of the array, not the valid and exact number of elements. Using for loop with respect to Perl array length gives the clearest way by counting the value that Perl already knows.
Syntax:
There are two ways that are used for calculating, which are represented as follows :
$ arr_length = @fruit;
This is the first way to determine the Perl Array length by assigning a simple value to the scalar variable of the array, as shown above.
Where arr_length is the variable considered, and @fruit is the value being assigned.
Another way is to explicitly convert the scalar value into some value that will determine the Perl Array length, as shown.
$ arr_length2 = scalar @fruit
How to calculate array length in Perl?
Array length is an important topic of concern when It comes to calculating the array length with respect to implicit or explicit conversions. There are certain ways in which array length is calculated, which are as follows :
- It gives the proper number of elements with values, although being slow.
- It is also possible to get the length of the array within the hash, which also arises as a requirement quite often in that case; when calling a function, it is crucial to actively consider the type of array elements and their associated data types that need to be incorporated.
- autobox: the core is the module to use array within the hash, but using the module itself is not much recommended and can destroy a lot of memory that must be managed efficiently.
- Nested array length can also be manipulated and calculated. In that case, dereferencing syntax exists for both the implicit and explicit conversion, although the former one is more recommended, as mentioned before, while calculating the array length.
Examples
Let us discuss examples of array length.
Example #1
This program demonstrates the declaration of the array followed by storing the value and performing implicit conversion by returning the value as shown and then converting the value using explicit conversion, which returns the following output as shown.
Code:
@arr_p = (10, 8, 14, 20, 86, 60);
$impSize_1 = @arr_p;
$expSize_2 = scalar @arr_q;
print "Size of implicit_sized array $impSize_1\n";
print "Size of explicit_sized array $expSize_2";
Output:
Example #2
This program demonstrates the array declaration, which after calculating the length of the array with its size, returns the physical size of the array, not the actual valid number of elements as shown in the output.
Code:
#!/usr/bin/perl
@arr_p = (4,8,10,12);
$arr_p[62] = 8;
$arr_size = @arr_p;
$max_indx = $#arr_p;
print "Size_1: $arr_size\n";
print "Max_indx: $max_indx\n";
Output:
Example #3
This program demonstrates the declaration of an array with a specified size. The array is initialized with indexed values. The program outputs the physical size of the array, ensuring that only the required values are present.
Code:
@arr_0 = ("one_1", "two_2", "three_3");
for($lp_indx = 0; $lp_indx <= $#arr_0; $lp_indx++) {
print $arr_0[$lp_indx];
}
Output:
Example #4
This program demonstrates one of the ways to find and print a array size using the index of the last element in the defined array + 1 as shown in the output.
Code:
@fruits = qw(orange Kiwi banana);
$size_3 = $#fruits + 1;
printf("The sizes for fruits is : %d,\n", $size_3);
Output:
Example #5
This program demonstrates the usage of array size with explicit scalar conversion and prints the values after conversion, as shown in the output.
Code:
@fruits = qw(orange Kiwi banana guava grapes);
$size_2 = scalar @fruits;
printf("The sizes for fruits is : %d,\n", $size_2);
Output:
Conclusion
Programmers rely on the length of a Perl array for various purposes. In Perl, determining the length of an array is essential for manipulating it effectively. By knowing the array length, programmers can determine the appropriate approach for calculations and accessing array elements.
Recommended Articles
This is a guide to Perl array length. Here we also discuss the Definition and How to calculate array length in Perl? along with different examples and code implementation. You may also have a look at the following articles to learn more –