List-Manipulation Functions

There are two groups of functions that manipulate lists and arrays

• The functions that manipulate standard array variables and their lists
• The functions that manipulate associative arrays

Array and List Functions

The following functions manipulate standard array variables and the lists that they store

 grep extract the elements of a list that match a specified pattern splice enables you to modify the list stored in an array variable, by passing appropriate arguments to splice, you can add elements to the middle of a list, delete portion of a list, or replace a portion of a list. shift remove an item from the beginning a list unshift put an item at the beginning of a list push put an item to the end of a list pop remove an item from the end of a list split split a character string into a list of elements sort/reverse sort a list alphabetically reverse reverse the order of a list map enables you to use each element of a list, in turn as an operand in an expression wantarray wantarray determines if the calling function wants a scalar or array variable returned Examples grep # grep (, ) \$line = "This line of input contains 8, 11 and 26."; @words = split(/\s+/, \$line); @numbers = grep(/^\d+[.,;:]?\$/, @words); print ("Numbers: @numbers"); Note: you can also use grep with the file-test operators opendir(CURRDIR, ".") || die("Can't open current directory"); @filelist = grep(!/^\./, grep(-r, readdir(CURRDIR))); print ("@filelist"); splice # retval = splice(, , , ); # Replace @array = qw(1 2 3 4); splice (@array, 1, 2, ("two", "three"));        # list will now be (1 two three 4); # Appending splice(@array, 3, 0, "hello", "there");         # add to position 3 shifting anythnig after splice(@array, @array, 0, "hello", "there");    # add to the end of the list # Deleting splice(@array, 2, 2);                           # delete two elements from the offset shift/unshift ## add or delete from the left side of the array, element 0 @array = qw(1 2 3 4); \$first_element = shift(@array); print ("Array: @array"); print("\nFirst element:" . \$first_element); unshift(@array, \$first_element); print ("\nArray: @array"); push/pop ## add or delete from the right side of the array, last element @array = qw(1 2 3 4); \$first_element = pop(@array); print ("Array: @array"); print("\nFirst element:" . \$first_element); push(@array, \$first_element); print ("\nArray: @array"); split \$string = "one::two::three::four::five::six"; @words = split(/::/, \$string); @limit_words = split(/::/, \$string, 3);         # specify a maximum number of elements print ("@words"); print ("@limit_words"); sort @array = qw( one two three four five six); @sorted = sort(@array); print ("@sorted\n"); reverse @array = qw( one two three four five six); @reversed = reverse(@array); print("@reversed"); map # map(, );        # The map function uses the system variable \$_ for each element @array = qw(100 200 300 400); @results = map(\$_+1, @array); print("@results"); wantarray @array = &mysub();                   # using an array the wantarray will return true \$scalar = &mysub();                  # using an scalar the wantarray will return false sub mysub {    if (wantarray()) {       print("Returning type is an array - TRUE\n");    } else {       print("Returning type is an scalar - FALSE\n");    } }

Here are some equivalent comparisions using the splice command assuming `(\$[ == 0 and \$#a >= \$i` )

 Add an item on the end of a list push(@a,\$x) splice(@a,@a,0,\$x) Remove an item from the end of a list pop(@a) splice(@a,-1) Remove an item from the beginning of a list shift(@a) splice(@a,0,1) Add an item to the beginning of a list unshift(@a,\$x,\$y) splice(@a,0,0,\$x,\$y) set a element array to a value \$a[\$x] = \$y splice(@a,\$x,1,\$y)

To create a queue you would use push and shift, and to create stack you would use push and pop.

Associative Array Functions

The following functions manipulate associative arrays

 keys returns a list of subscripts of the element of an associative array values returns a list consisting of all the values in an associate array each returns an associative element as a two element list delete deletes an associative array element exists enables you to determine whether a particular element of an associative array exists. Examples keys %array = ("foo", "26", "bar", "27"); @keys = keys(%array); print("@keys\n"); # keys is commonly used as below foreach \$i (keys (%array)) {    print \$i . "\n"; } Note: in no particular order will the list be returned values %array = ("foo", "26", "bar", "27"); @values = values(%array); print("@values\n"); foreach \$i (values (%array)) {    print \$i . "\n"; } Note: in no particular order will the list be returned each %array = ("foo", "26", "bar", "27"); @each = each(%array); print("@each\n"); foreach \$i (each (%array)) {    print \$i . "\n"; } Note: in no particular order will the list be returned, also do not use delete when using each, because the behavior of each unpredictable delete %array = ("foo", "26", "bar", "27"); \$retval = delete(\$array{"foo"});        # returns the deleted elements value 26 foreach \$i (keys (%array)) {    print \$i . "\n";                     # only bar should exist } print \$retval; exists %array = ("foo", "26", "bar", "27"); if ( exists(\$array{"foo"}) ) {    print("Foo exists"); } else {    print ("Foo does NOT exists"); }

When using associative arrays do not use push, pop, shift or splice because the position of any particular element in the array is not guaranteed.