The usage of Perl push and the reflection of subroutine return value _perl

Source: Internet
Author: User
Tags reflection

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";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.