Updated March 31, 2023
What is Perl Array?
Perl array is defined as a variable that stores the ordered list of scalar values, the array variable in Perl language starts with “at” (@) sign. If we initialize an array element we need to use dollar ($) sign in Perl language. This array used to store the list of values or objects and each value in the array is known as elements of an array, an array is a special type of variable in this language. We can declare an array element of any scalar values like the number, string, and floating-point numbers.
How to Define Array in Perl?
We can define any number, string, and floating-point value to the array. Below is the example of define array in for the same is as follows. We need to use a variable name at the time of defining array in for the same.
Code:
@Array1 = (20, 25, 30); # Defining Array1 for scalar integer type variables.
@Array2 = ('ABC', 'PQR', 'XYZ'); # Defining Array2 for scalar String type variables.
@Array3 = (10.1, 10.2, 10.3); # Defining Array3 for scalar floating point type variables.
@Days_of_week = qw/SUN MON TUE WED THU FRI SAT/; # Defining array using qw.
use warnings;
if (1<2)
{
print "\$Array1[0] = $Array1[0]\n";
print "\$Array1[1] = $Array1[1]\n";
print "\$Array1[2] = $Array1[2]\n";
print "\$Array2[0] = $Array2[0]\n";
print "\$Array2[1] = $Array2[1]\n";
print "\$Array2[2] = $Array2[2]\n";
print "\$Array3[0] = $Array3[0]\n";
print "\$Array3[1] = $Array3[1]\n";
print "\$Array3[2] = $Array3[2]\n";
print "$Days_of_week[0]\t $Days_of_week[1]\t $Days_of_week[2]\t $Days_of_week[3]\t $Days_of_week[4]\t $Days_of_week[5]\t $Days_of_week[6]\t";
}
Output:
Explanation: In the above example, we have to define three array-like array1, array2, and array3. Array1 is for integer type variable, array2 is for string type variables and array3 is for floating-point number variables.
How to Initialize Array in Perl?
We can initialize array variables as integer, string and floating-point numbers. We can also initialize an array of scalar data types in a single element. We need to use $ (Dollar) sign starting to the array while initializing an array in for the same language. Below is the example of initializing the array in Perl are as follows.
Code:
@Array11 = (10, 20, 30);# Defining Array1 for scalar integer type variables.
@Array22 = ('ABC', 'PQR', 'XYZ');# Defining Array2 for scalar String type variables.
@Array33 = (10.1, 10.2, 10.3);# Defining Array3 for scalar floating point type variables.
@Array44 = (40, 'ABCD', 10.4);# Defining Array44 for all scalar data types variables.
use warnings;
if (1<2)
{
print "\$Array11[0] = $Array11[0]\n";
print "\$Array11[1] = $Array11[1]\n"; # Initializing Array11 element.
print "\$Array11[2] = $Array11[2]\n";
print "\$Array22[0] = $Array22[0]\n";
print "\$Array22[1] = $Array22[1]\n"; # Initializing Array22 element.
print "\$Array22[2] = $Array22[2]\n";
print "\$Array33[0] = $Array33[0]\n";
print "\$Array33[1] = $Array33[1]\n"; # Initializing Array33 element.
print "\$Array33[2] = $Array33[2]\n";
print "\$Array44[0] = $Array44[0]\n";
print "\$Array44[1] = $Array44[1]\n"; # Initializing Array44 element.
print "\$Array44[2] = $Array44[2]\n";
}
Output:
Explanation: In the above example, we have initialized and declare array11 for integer type scalar data types, array22 for string scalar data types and array33 for floating-point number scalar data types. We have initialized and declared an array of array44 as the combination of all scalar data types variables.
How to Access Array elements in Perl?
We can access single elements or objects from the full array element. We can access a single element using a dollar ($) in Perl. Before accessing an element from the array we can use dollar sign before accessing and after dollar sign, we need to append the index of that variables. Below is the example of the access array element is as follows.
Code:
@Array11 = (10, 20, 30); # Defining Array11 for scalar integer type variables.
@Array22 = (40, 'ABCD', 10.4); # Defining Array22 for all scalar data types variables.
@Months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; # Defining array of month by using qw.
@Array33 = ('AB', 'PQ', 'XY'); # Defining Array33 for scalar String type variables.
use warnings;
if (1<2)
{
print "\$Array11[0] = $Array11[0]\n";
print "\$Array11[1] = $Array11[1]\n"; # Accessing Array11 element.
print "\$Array11[2] = $Array11[2]\n";
print "\$Array22[0] = $Array22[0]\n";
print "\$Array22[1] = $Array22[1]\n"; # Accessing Array22 element.
print "\$Array22[2] = $Array22[2]\n";
print "$Months[0]\t $Months[1]\t $Months[2]\t $Months[3]\t $Months[4]\t $Months[5]\t $Months[6]\t $Months[7]\t $Months[8]\t $Months[9]\t $Months[10]\t $Months[11]\t";
# Accessing array element of months variables.
print "$Months[11]\n";
print "$Array33[0]\t $Array33[1]\t $Array33[2]\t"; # Accessing element in single line.
}
Output:
Explanation: In the above example, we have access array element of array11, array22, months and array 33.
Types of Array in Perl
Below are the types of an array that are as follows.
- Pop
- Shift
- Push
- Unshift
- Splice
1. Pop
The pop array is removed from the last element of an array. Below is the example of a pop array.
Code:
@Array = ("AB", "PQ", "XY");
pop @Array;
print "@Array\n";
Output:
2. Push
Push array is appended new element in an array. Below is the example of a push array.
Code:
@Array = ("AB", "PQ", "XY");
push @Array, "CD";
print "@Array\n";
Output:
3. Shift
Shift array will remove the left element of an array. Below is the example of the shift array.
Code:
@Array = ("AB", "PQ", "XY");
shift @Array;
print "@Array\n";
Output:
4. Unshift
Unshift array will add new elements starting to the array. Below is the example of the Unshift array.
Code:
@Array = ("AB", "PQ", "XY");
unshift @Array, "CD";
print "@Array\n";
Output:
5. Splice
Splice will remove and replace the element as defined. Below is the example of the splice is as follows.
Code:
@Number = (1..10);
print "Before - @Number\n";
splice(@Number, 9, 10, 11..12);
print "After - @Number\n";
Output:
Advantages
Below are the advantages are as follows.
- Multiple data items are accessed by using an array.
- Using array we can save the memory.
- Using an array in Perl debugging of code is easy.
- It is efficient to declare array while defining a single element.
- The array is used to help increase the reusability of code.
Conclusion
Perl array used to store a list of values and each value in the array is known as elements of an array, the array is a special type of variable language. We can declare an array element of any scalar values like a number. The array is the most important and useful in Perl.
Recommended Articles
We hope that this EDUCBA information on “Perl Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.