The pop operation takes the last element of the array out and returns:
@array =5..9;
$fred =pop (@array); # $fred Get 9, @array now (5,6,7,8)
$barney =pop@array; # $barneygets 8, @array now (5,6,7)
Pop@array; # @array Now (5,6) (7 is discarded)
In the last example, a pop is used in "Inavoidcontext", which means there is no place to store its return value. It is legal to use POPs in this way.
If the array is empty, the pop does nothing (because no elements can be moved out) and returns to undef.
You may have noticed that POPs can be used or not with parentheses. This is a general rule in Perl: If you remove the parentheses, the parentheses are optional.
Of The opposite of a pop is a push, which adds an element (or a list of elements) to the end of the array:
Educated people will find that this is synonymous with repetition.
Push (@array, 0); # @array for now (5,6,0)
push@array,8; # @array for now (5,6,0,8)
push@array,1.. 10; # @array 10 more elements now.
@others =QW/9 0 2 1 0/;
Push@array, @others; # @array now has 5 more elements (19 in total)
The first parameter of the push or the unique parameter of the pop must be an array variable.
Copy Code code as follows:
#!/bin/perl
Sub Above_average
{
$number =@_;
foreach $how (@_)
{
$total = $total + $how;
}
$the _average= $total/$number;
foreach (@_)
{
if ($_> $the _average)
{
Push (@larger, $_) #这里不用赋值, add the array element, just push it.
}
}
@larger, #子程序的返回值, be sure to have it, it's not written at first.
}
Print "Please input several numbers,and you'll get the number which is large than their average\n";
@the_number_input =<stdin>;
@the_number_larger =above_average (@the_number_input);
print "@the_number_larger \ n";