Perl Arrays

  1. An array is a variable that stores an ordered list of scalar values. 
  2. Array variables are preceded by an "at" (@) sign. 
  3. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.
1) Declare an array and assign values 
1
my @names = ("Foo", "Bar", "Baz");

2) Simple example of using the array variables.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl

#Array variables are populated using either parentheses or the qw operator.
@ages = (43, 32, 29);             
@names = ("Jerry", "Albert", "Tom");
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@array = qw/This is an array/;

#Sequential numbers and letters
@numbersUpTo_10 = (1..10);
@characters_AToZ = (A..Z);

#Accessing Array Elements

print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";

print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";

print "\$days[0] = $days[0]\n";
print "\$days[1] = $days[1]\n";
print "\$days[2] = $days[2]\n";

print "@numbersUpTo_10\n";
print "@characters_AToZ\n";
Output:
$ages[0] = 43
$ages[1] = 32
$ages[2] = 29
$names[0] = Jerry
$names[1] = Albert
$names[2] = Tom
$days[0] = Mon
$days[1] = Tue
$days[2] = Wed
1 2 3 4 5 6 7 8 9 10
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

3) Add and remove elements in an array
Push: Appends a new element at the end of the array.
Pop: Removes the last element from the array
Shift: The shift array function removes the left most element of array

Unshift: adds a new element at the start of the array
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/perl

# Create an array
@countries = ("India","USA","Canada","Ireland");
print "1. \@countries  = @countries\n";

# Add one element at the end of the array
push(@countries, "England");
print "2. \@countries  = @countries\n";

# Add one element at the beginning of the array
unshift(@countries, "China");
print "3. \@countries  = @countries\n";

# Remove one element from the last of the array.
pop(@countries);
print "4. \@countries  = @countries\n";

# remove one element from the beginning of the array.
shift(@countries);
print "5. \@countries  = @countries\n";
Output:
1. @countries  = India USA Canada Ireland
2. @countries  = India USA Canada Ireland England
3. @countries  = China India USA Canada Ireland England
4. @countries  = China India USA Canada Ireland
5. @countries  = India USA Canada Ireland

4) Extract a "slice" from an array so that you can select more than one item from an array in order to produce another array.
1
2
3
4
5
6
7
8
#!/usr/bin/perl

@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec/;
@summar = @months[2,3,4];
@Rain = @months[5..7];

print "@summar\n";
print "@Rain\n";
Output:
Mar Apr May
Jun Jul Aug

5) Replacing Array Elements using splice()
1
2
3
4
5
6
7
#!/usr/bin/perl

@nums = (1..20);
print "Before - @nums\n";

splice(@nums, 5, 5, 21..25); 
print "After - @nums\n";
Output: 
Before - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20

6) Split a string into an array of strings using split() function.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl

# define Strings
$string = "The world is beautiful";
$animals = "dog,cat,bear,elephant,giraffe";

# transform above strings into arrays.
@new_string = split(' ', $string);
@new_animals  = split(',', $animals);

print "$new_string[3]\n";  # This will print beautiful
print "$new_animals[2]\n";   # This will print bear
Output:
beautiful
bear

7) Join the array elements and create a scalar string using join() function.
1
2
3
4
5
#!/usr/bin/perl

@array =("The","world","is","beautiful");
$string = join( ' ', @array );
print "$string \n"
Output: The world is beautiful

8) Sort an array using sort() function.
1
2
3
4
5
6
7
8
9
#!/usr/bin/perl

# define an array
@names = qw(Richard Tom Andy Steve John);
print "Before Sorting: @names\n";

# sort this array
@names = sort(@names);
print "After Sorting: @names\n";
Output:
Before Sorting: Richard Tom Andy Steve John
After Sorting: Andy John Richard Steve Tom

9) Array Size, Merge array and Iterate using for-each loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/perl

@num = (1,2,3,4,5,6,7,8,9,10);
@char = qw(A B C);

#Size
print $size=scalar (@num);

#Iterate arrays using forEach loop
print ("\nPrinting array using ForEach loop\n");
foreach $i (@num){
 print ("$i ")
}

#Merging an arrays
@merged = (@num, @char);
print ("\nMerged = @merged\n");
Output:
10
Printing array using ForEach loop
1 2 3 4 5 6 7 8 9 10
Merged = 1 2 3 4 5 6 7 8 9 10 A B C


<-- Previous || Next -->

1 comment: