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:


Perl Simple Array Example

This is a simple example to use Perl array.

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.


Perl Array Accessing

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.

Output:

Jan
Feb
Aug
Oct
Jul
Oct

Perl Array Size or 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.

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

1) Push on Array

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

Output:

pink red blue orange

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


2) Pop on Array

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

Output:

pink red 

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


3) Shift on Array

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

Output:

red blue

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


4) Unshift on Array

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

Output:

orange pink red blue 

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


Perl Replacing Array Elements, splice()

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

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.


Perl Strings to Arrays, split()

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

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.


Perl Arrays to Strings, join()

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

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.


Perl Merging Two Arrays, merged()

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

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.


Perl Sorting Arrays, sort()

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

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.