There are several functions working on arrays:
pop and push implement a LIFO stack.
pop fetches the last element of the array
returns that value and the array becomes one shorter
if the array was empty pop returns undef
LAST = pop ARRAY;
push is the opposite of pop it adds element(s) to the end of the array
It returns number of elements after the push.
PUSH ARRAY, SCALAR, ... (more SCALARs);
Example: Example 8-4. examples/arrays/pop_push.pl #!/usr/bin/perl
use strict;
use warnings;
my @names = ("Foo", "Bar", "Baz");
my $last_name = pop @names;
print "$last_name\n"; # Baz
print "@names\n"; # Foo Bar
push @names, "Moo";
print "@names\n"; # Foo Bar Moo
If you are interested in on-site trainings by the author, please
contact me directly.
|