Programming Perl

Programming PerlSearch this book
Previous: 3.2.111 printfChapter 3
Next: 3.2.113 q/STRING/

3.2.112 push


This function treats ARRAY as a stack, and pushes the values of LIST onto the end of ARRAY. The length of ARRAY increases by the length of LIST. The function returns this new length. The push function has the same effect as:

foreach $value (LIST) {
    $ARRAY[++$#ARRAY] = $value;


splice @ARRAY, @ARRAY, 0, LIST;

but is more efficient (for both you and your computer). You can use push in combination with shift to make a fairly time-efficient shift register or queue:

for (;;) {
    push @ARRAY, shift @ARRAY;

See also pop and unshift.

Previous: 3.2.111 printfProgramming PerlNext: 3.2.113 q/STRING/
3.2.111 printfBook Index3.2.113 q/STRING/