Count the Array Length in PERL


“The length of an array indicates the number of elements of the particular array. Many ways exist in PERL to calculate the array length. When the length of the array is calculated by using the “@” symbol, then it is called an implicit scalar conversion. When the length of the array is calculated by using the scalar keyword, then it is called an explicit scalar conversion. The “#” symbol can also be used to count the length of the array. Different ways of counting the array length and using it in the PERL scripts have been shown in this tutorial.

Different ways of counting the array length have been described in the next part of this tutorial by using examples.”

Example-1: Count the Array Length Using the “@” Symbol

Create a PERL file with the following code that will count the length of an array by using the “@” symbol. An array of 5 string values has been defined in the code. The total number of array values has been stored into a variable named $length that has been printed later.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Count the array length
$length = @myArr;
# Display the array length
print «The array contains $length elements.n«;

Output

The following output will appear after executing the above code.

Example-2: Count the Array Length Using the Scalar Keyword

Create a PERL file with the following code that will count the length of an array by using the scalar keyword. An array of 6 numbers have been defined in the code. The scalar keyword has been used to count the total number of elements of the array. Next, two values were added to the array and again counted, the total number of elements of the array. The join() function has been used to print the array values with the space.

# Define an array of numbers
@numbers = (6, 5, 3, 1, 2, 8);
# Print the array values
print «Array values are:n«, join(‘ ‘,@numbers), «n«;
# Print the total number of elements of the array
print «Total elements of the array is «, scalar @numbers, «n«;
#Add two new elements
$numbers[@numbers] = 9;
$numbers[@numbers] = 4;
# Print the array values after adding two values into the array
print «Array values after adding two elements are:n«,join(‘ ‘,@numbers), «n«;
# Print the length of the array after insertion
print «Total elements of the array is «, scalar @numbers, «n«;

Output

The following output will appear after executing the above code. The total number of elements of the array after adding two elements is 6+2 = 8.

Example-3: Count the Array Length Using the “#” Symbol

Another way of counting the total number of elements of the array is using the “#” symbol. The last index of the array is counted by using the “#” symbol. So the total array elements can be counted by adding 1 with the last index value. Create a PERL file with the following code that will count the array length by using the “#” symbol. An array of 5 float numbers has been defined in the code. The pop() function has been used to remove 3 elements from the array. The join() function has been used to print the array values with the space.

# Declare an array of float numbers
@float_num = (45.89, 34.12, 56.34, 90.34, 45.23);
# Print the array values
print «Array values:n«, join(‘ ‘,@float_num), «n«;
# Print the length of the array
print «Total number of array elements : «, $#float_num+1, «n«;
# Remove three elements from the array
pop @float_num;
pop @float_num;
pop @float_num;
# Print the array values
print «Array values after removing three elements:n«, join(‘ ‘,@float_num), «n«;
# Print the length of the array after removing three values
print «Total number of array elements after removing the elements: «, $#float_num+1, «n«;

Output

The following output will appear after executing the above code. The total number of elements of the array after removing 3 elements is 5-3 = 2.

Example-4: Print the Array Values Using Array Length

Create a PERL file with the following code that will use the length of the array to iterate the values of the array using for loop and print the array values in each line. The last index value of the array has been used here to count the length of the array.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Iterate the array values based on the array length
for($i = 0; $i < $#myArr+1; $i++)
{
# Print the array values
print $myArr[$i], «n«;
}

Output

The following output will appear after executing the above code.

Example-5: Count the Array Length Using Loop

Another simple way to count the array length is by using any loop. Create a PERL file with the following code that will count the array length by using the foreach loop. An array of 5 string values has been defined here. A counter variable has been used in the code to count the length of the array by incrementing the counter value by 1 in each iteration of the loop.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Initialize the counter
$counter = 0;
print «Array values:n«;
# Iterate the array values based on the array length
foreach $val (@myArr)
{
# Print the array values
print $val, «n«;
$counter++;
}

# Print the length of the array
print «nTotal number of array elements : «, $counter, «n«;

Output

The following output will appear after executing the above code.

Conclusion

The way of counting the array values in PERL has been shown in this tutorial by using multiple examples. The PERL user can use any of the ways to find out the array length and use it for solving PERL programming problems.



Source link