perl语言,state好像失效了use 5.0.10;use feature 'state';sub average{ my $sum=0; foreach(@_){ $sum+=$_; } my $average=$sum/@_; return $average;}sub above_average{ my $num=0; my $average=&average(@_); my @
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 08:08:27
perl语言,state好像失效了use 5.0.10;use feature 'state';sub average{ my $sum=0; foreach(@_){ $sum+=$_; } my $average=$sum/@_; return $average;}sub above_average{ my $num=0; my $average=&average(@_); my @
perl语言,state好像失效了
use 5.0.10;
use feature 'state';
sub average{
my $sum=0;
foreach(@_){
$sum+=$_;
}
my $average=$sum/@_;
return $average;
}
sub above_average{
my $num=0;
my $average=&average(@_);
my @list=undef;
foreach(@_){
if($_>$average){unshift(@list,$_);}
}
return @list;
}
sub greet{
state $namelast;
state $namenow;
if($namenow==undef){
$namelast=undef;
$namenow=shift(@_);
print "Hi $namenow","!","You are the first one here\n";
}
else{
$namelast=$namenow;
$namenow=shift(@_);
print "Hi $namenow","!","$namelast is also here ";
}
}
greet("Fred");
greet("Barney");
greet("Tom");
结果:
perl语言,state好像失效了use 5.0.10;use feature 'state';sub average{ my $sum=0; foreach(@_){ $sum+=$_; } my $average=$sum/@_; return $average;}sub above_average{ my $num=0; my $average=&average(@_); my @
sub greet{
state $namelast;
state $namenow;
if(! defined $namenow ){ -->这里注意undef不是用等号判断的奥
$namelast=undef;
$namenow=shift(@_);
print "Hi $namenow","! ","You are the first one here\n";
}
else{
$namelast=$namenow;
$namenow=shift(@_);
print "Hi $namenow","! ","$namelast is also here \n";
}
}