In this chapter, I'll discuss some frequently performed functions on Perl arrays.
Let's consider this array
@fruit = ("apple","banana","pineapple","mango","guava");
push @fruit,"plum";
Adds an element to the end of the array.
my $new_fruit = shift @fruit;
The above statement removes the first element of @fruits and assigns it to $new_fruit.
my $popped_fruit = pop @fruit;
The above line pops (removes last element from the list) and assigns it to $popped_fruit.
Use of @_ in a subroutine
@_ is the default array that is passed to a subroutine.
See the following two subroutines and what they do
sub test{
($name,$surname,$age)=@_;
}
We can assign three scalars of a subroutine with the values of the arguments to the subroutines. @_ is the array of argument
s passed to the subroutine.
sub test2{
$name = shift;
$surname=shift;
$age=shift;
}
If these arguments are not going to be required for further processing, the default array can be shift'ed and the assignment
be made.
Monday, April 10, 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment