Arrays in Perl

walden systems, geeks corner, developer, perl, code, authentication, active directory, array, split, join, slice, elements, scalar
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

Perl stands in for Practical Extraction and Reporting Language. Perl was created by Larry Wall in 1987 while being employed on a bug reporting system and AWK. He is still the chief architect and developer of Perl. Perl is a programming language designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, and web development.

Creating an array

An array is a variable that stores an ordered list of scalar values. In Perl, array variables are preceded by an @ sign. When referring to a single element of an array, Perl uses the $ with the variable name followed by the index of the element in square brackets. In Perl, List and Array terms are often used as if they're interchangeable. But the list is the data, and the array is the variable. To create an array in Perl, we use the @ sign and we can use either parentheses or the qw keyword. We can assign variables all at once or we can assign each value individually. If we want to add a sequential number and letters, Perl offers us a shortcut using .. so rather than typing out each element, we can use a range.

1   @array1 = ( 1, 2 ,3, 4 ) l
2   @array2 = qw/A new array/ ;
3   $array3[0] = "Hello" ;
4   $array3[1] = "World" ;
5   @array4 = (1..5) ;
6   @array5 = (a..d) ;


Line 1 creates an array, array1, with 4 elements: 1, 2, 3, 4.
Line 2 creates an array, array2, with 3 elements: "A", "new", "array".
Lines 3 and 4 creates an array, array3, with 2 elements: "Hello", "World". Line 5 creates an array, array4, with 5 elements: 1, 2, 3, 4, 5.
Line 6 creates an array, array5 with 4 elements: "a", "b", "c", "d".


Accessing array elements

When accessing individual elements from an array, we must prefix the variable with a $ and then append the element index within the square brackets after the name of the variable. Array indices start from zero, so to access the first element we need to give 0 as an index. In Perl, we can also give a negative index, in which case we select the element from the end, rather than the beginning, of the array.

    @array1 = (a..z) ;
    print "$array2[4], " ;
    print "$aray2[-3] ;


Will give us the output: e, x.

Adding and removing elements in an array

Perl provides a number of useful functions to add and remove elements in an array. We can add elements to the end of an array using the push function. We can remove and return the last element of the array using the pop function. We can shorten the array to the left by one using the shift function. We can add an elements to the beginning of the array using the unshift function. We can select more than one element of an array by entering valid indices or a range of indices.

1    @array1 = ( 1, 2, 3 ) ;
2    push ( @array1, 4 ) ;
3    unshift ( @array1, 0 ) ;
4    pop ( @array1 ) ;
5    shift ( @array1 ) ;
6    @array2 = @array1 ( 1, 2 ) ;
7    @array3 = @array1( 0..2 );


After execution of line 2, array1 holds 1, 2, 3 ,4.
After execution of line 3, array1 holds 0, 1, 2, 3, 4.
After execution of line 4, array1 holds 0, 1, 2, 3.
After execution of line 5, array1 holds 1, 2, 3.
After execution of line 6, array2 holds 2, 3.
After execution of line 7, array3 holds 1, 2.

Other functions

There are other functions to manipulate array in Perl. To get the size of the array, we can use the scalar context. This will not give us the number of valid arguments in the array, only the total size of the array. The splice function will remove the elements of an array designated by an offset and length, and replaces them with a list, if specified. Finally, it returns the elements removed from the array. The split function splits a string into an array of strings, and returns it. If a limit is specified, splits into at most that number of fields. If a pattern is omitted, it splits on whitespace. The join function joins the separate strings of LIST into a single string with fields separated by the value of a expression, and returns the string.

splice

    @array1 = ( 1..10 );
    splice ( @array1, 5,5, 21..25 );


array1 now holds 1 2 3 4 5 21 22 23 24 25.


split
    $string1 = "Hello John Doe" ;
    @array1 = split( ' ', $string1 ) ; 


array1 holds 3 elements: "Hello", "John", and "Doe".


join
    @array1 = ("Hello",  "John", "Doe" ) ;
    @string1 = join( ' ', @array1 ) ; 


string1 contains the string, "Hello John Doe" )