Perl Array

A Perl array variable stores an ordered list of scalar values.

To refer a single element of Perl array, variable name will be preceded with dollar ($) sign followed by index of element in the square bracket.

Syntax
@arrayName = (element1, element2, element3..);
Example

This is a simple example to use Perl array.

snippet
#!/usr/bin/perl

@num = (2015, 2016, 2017);             
@string = ("One", "Two", "Three");
print "$num[0]\n";
print "$num[1]\n";
print "$num[2]\n";
print "$string[0]\n";
print "$string[1]\n";
print "$string[2]\n";
Output
2015 2016 2017 One Two Three

In the above example, we have defined two arrays, one with number element and other with string element. Both arrays are printed with their index elements.

Accessing Array

To access a single element of a Perl array, use ($) sign before variable name. You can assume that $ sign represents singular value and @ sign represents plural values.

Variable name will be followed by square brackets with index number inside it. Indexing will start with 0 from left side and with -1 from right side.

Example
snippet
@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec/;
print "$months[0]\n";
print "$months[1]\n";
print "$months[7]\n";
print "$months[9]\n";
print "$months[6]\n";
print "$months[-3]\n";
Output
Jan Feb Aug Oct Jul Oct

Array Size/ Length

The size of an array is determined with scalar context on the array. The returned value will be always one greater than the largest index. In short the size of an array will be ($#array + 1). Here, $#array is the maximum index of the array.

Example
snippet
@array = (you, me, us);
$array[5] = 4;
$size = @array;
$index_max = $#array;
print "Size:  $size\n";
print "Maximum Index: $index_max\n";
Output
Size: 6 Maximum Index: 5

In the output, there are only three elements containing information, but the give array has total 5 elements.

Perl Array Functions

You can add or remove an element from an array using some array functions.

We'll discuss following array Perl functions:

  • Push
  • Pop
  • Shift
  • Unshift
Push

The push array function appends a new element at the end of the array.

Example
snippet
@array = ("pink", "red", "blue");
push @array, "orange";
print "@array\n";
Output
pink red blue orange

In the above program, "orange" element is added at the end of the array.

Pop

The pop array function removes the last element from the array.

Example
snippet
@array = ("pink", "red", "blue");
pop @array;
print "@array\n";
Output
pink red

In the above program, "blue" element is removed from the end of the array.

Shift

The shift array function removes the left most element of array and thus shorten the array by 1.

Example
snippet
@array = ("pink", "red", "blue");
shift @array;
print "@array\n";
Output
red blue

In the above program, "pink" is removed from the array.

Unshift

The unshift array function adds a new element at the start of the array.

Example
snippet
@array = ("pink", "red", "blue");
unshift @array, "orange";
print "@array\n";
Output
orange pink red blue

In the above program, "orange" is added at the start of the array.

splice() - Replacing Array Elements

The splice array function removes the elements as defined and replaces them with the given list.

Example
snippet
@alpha = (A..Z);
print "Before - @alpha\n";
splice(@alpha, 8, 8, U..Z); 
print "After - @alpha\n";
Output
Before - 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 After - A B C D E F G H U V W X Y Z Q R S T U V W X Y Z

In the above program, the replacement begins counting from 9th position (I) to 8 elements that is P.

split() - Strings to Arrays

With the help of split() function, we can split a string into array of strings and returns it.

Example
snippet
# original string
$string = "Where-There-Is-A-Will-There-Is-A-Way";
# transforming strings into arrays.
@string = split('-', $string);
print "$string[4]\n";
Output
Will

In the above program, we have transformed $string into array at hyphen (-) values. Now from this array, we have printed fourth element of the array.

join() - Arrays to Strings

The join() function is used to combine arrays to make a string. It combines the separate arrays into one string and returns it.

Example
snippet
# original string
$string = "Where-There-Is-A-Will-There-Is-A-Way";
# transforming arrays into strings.
@string = split('-', $string);
$string_full = join( '-', @string );
print "$string_full\n";
Output
Where-There-Is-A-Will-There-Is-A-Way

In the above program, the string is splitted at hyphens (-). We have used join() in $string_full and printed it.

merged() - Merging Two Arrays

Two arrays can be merged together using merged() function as a single string removing all the commas in between them.

Example
snippet
#two arrays
@array1 = ("Girl", "in", "front", "of", "me");
@array2 = ("is", "very", "beautiful");
#merging both the arrays
@merged = (@array1, @array2);
print "@merged\n";
Output
Girl in front of me is very beautiful

In the above program, array1 and array2 are merged into one single string and then printed.

sort() - Sorting Arrays

To sort an array, sort() array function is used. The sort() function sorts all the elements of an array according to the ASCII standard.

Example
snippet
# defining array
@days = ("sun", "mon", "tue", "wed", "thu", "fri", "sat");
print "Original array: @days\n";
# sorting array
@days = sort(@days);
print "Sorted array: @days\n";
Output
Original array: sun mon tue wed thu fri sat Sorted array: fri mon sat sun thu tue wed

In the above program, we have printed both original and sorted array. This array is sorted in the alphabetical order.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +