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 @
xUn@iHTc'*6HHdžv=vREB"!Vꦪ 3MRV8]!y;{|f[;><8T pp'!FQ5^&A#P"jOW 5Wh_qWy%ڙn۝ ^U &7jr 褘24wu:i{JL Kϝ>hY5AѰd:fbɔ0䄜Va6-651Iȉ%9=jȼlY.vJ@D_lfP+E,^TG(`qUCD}ԃ&Ʋ2J`5VgfL &갿K2yrNx;X)> `!sbl;,M)`c{إ =SU[>2U5 ^s;n6r;q@:Ղ`E}`_W Vx8NZg}e֬y 9lwj MQ"o-0$֨\8Ȣ%K5Y)H%(MQ֌&tDnT{ z#"M!u I,)7%$7H\\N5c) }9iist'AF w>}mnvppkw`9Kv@7#/dwRd '.QMs:+i=q\

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