1. Computing

Perl Array Push() Function - Quick Tutorial

How to Use the Array Push() Function

By

 $TOTAL = push(@ARRAY, VALUES); 
Perl's push() function is used to push a value or values onto the end of an array, which increases the number of elements. The new values then become the last elements in the array. It returns the new total number of elements in the array. It's easy to confuse this function with unshift(), which adds elements to the beginning of an array.
 @myNames = ('Larry', 'Curly');
 push(@myNames, 'Moe'); 
Picture a row of numbered boxes, going from left to right. The push() function would push the new value or values on to the right side of the array, and increase the elements. In the examples, the value of @myNames becomes ('Larry', 'Curly', 'Moe').

The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The push() function would push the value into the bottom of the stack, and increase the elements.

 @myNames = (
 'Larry',
 'Curly'
 );
 push(@myNames, 'Moe'); 
You can push multiple values onto the array directly:
 @myNames = ('Larry', 'Curly');
 push(@myNames, ('Moe', 'Shemp')); 
Or by pushing on an array:
 @myNames = ('Larry', 'Curly');
 @moreNames = ('Moe', 'Shemp');
 push(@myNames, @moreNames); 
  1. About.com
  2. Computing
  3. Perl
  4. Programming Perl
  5. Perl Push Function Reference - Learn How to Use Perl's Push() Function in This Quick Tutorial

©2014 About.com. All rights reserved.