Definition of Perl array length
Perl is the programming language where the length function within an array mostly gets used for strings only which are scalar in nature and put the array in scalar context by some other measures. 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 is 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 to 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 in Perl is an important topic of concern when It comes in calculating the array length in Perl with respect to implicit or explicit conversions. There are certain ways in which array length in Perl is calculated which are as follows :
- There is a way where the array size is calculated with implicit scalar conversion and also helps in making the overall array size linked for making the array spontaneous and ordered with respect to array indexing also so that it becomes a bit seamless to access the elements.
- It is most often recommended to calculate array length using implicit scalar conversion only as the variable considered will be within the memory aspect of the module.
- Another way to calculate size of the array is by Perl array size and its conversion using explicit scalar conversion which is not much recommended and used with respect to implicit scalar conversion.
- Third and final way to convert and calculate the size of array in Perl is by using the index of the last element and once the last element gets accessed then it is need to increment the value by adding one.
- Sometimes programmers also make use of for loop which is considered a slow way to access and manipulate with the array length of the elements but still gives the clearest way without much of the complexity and easily understandable loop with variables and accessibility.
- Using for statement is considered as a slow process for manipulating and playing around with the elements but that is not true 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 Perl hash which also arises as a requirement quite often in that case it is very much needed to keep in mind the type of array elements and its associated data types that needs to be incorporated while calling the function.
- autobox:core is the module to use array within the Perl hash but making use of the module itself is not much recommended and can destroy a lot of memory which needs to be managed efficiently.
- Indices of Array gets returned easily when Perl hash is used with the array for manipulation and accessibility of elements.
- Nested array length in Perl 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.
- All these ways can be used in any form but again depending upon the requirement and accessibility speed of the elements.
Examples
Let us discuss examples of Perl array length.
Example #1
This program demonstrates the declaration of 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 for 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 array with some size and then making the initialization of array with some indexed value and then by using for loop the unnecessary elements are removed internally and physical size with required values is returned as shown in the output.
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 Perl array size with 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 Perl array size with explicit scalar conversion and printing 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
Perl Array length is used for many things but determining the array length for manipulation is quite essential as It lets programmer get an idea about the type of approach needs to be followed for calculation and accessibility of elements with proper return type as it should return physical size of the array with proper and valid values.
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 its code implementation. You may also have a look at the following articles to learn more –