The shift and unshift Operators

The push and pop operators do things to the end of an array (or the right side of an array, or the portion with the highest subscripts, depending upon how you like to think about it).  Similarly, the unshift and shift operators perform the corresponding actions on the "start" of the array (or the "left" side of an array, or the portion with the lowest subscripts).  Here are a few examples:

	@array = qw# dino fred barney #;
	$a = shift(@array);			# $a gets "dino", @array now has ("fred", "barney")
	$b = shift(@array);			# $b gets "fred", @array now has ("barney");
	shift @array;				# @array is now empty
	$c = shift @array;			# $c gets undef, @array is still empty
	unshift(@array, 5);			# @array now has the one-element list (5)
	unshift(@array, 4);			# @array now has (4, 5)
	@others = 1..3;
	unshift @array, @others;		# @array now has (1, 2, 3, 4, 5)

Analagous to pop, shift returns undef if given an empty array variable.

Next:  The reverse Operator

OR

Main Menu for this topic