Wednesday, August 18, 2010

Push and Pop Operator

Push and pop functions are used to implement stack where new values are added to and removed from the right hand side of the list.

pop Operator:

Pop operator takes the last element off an array and returns it:

For example:

$array=5..9;                               #5 6 7 8 9
$last_element_1=pop(@array);   #gets 9

$last_element_2=pop @array;    # gets 8 ( pop operator works without parentheses also)

push Operator:

Push operator adds and element to the end of the array

For example:

@array=3..6;         #3 4 5 6
push(@array, 7);    #@array has 3 4 5 6 7
push @array, 8;     #@array has 3 4 5 6 7 8

No comments:

Post a Comment