加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Learning - 19 (last, next, redo, &&, ||

发布时间:2020-12-15 21:01:36 所属栏目:大数据 来源:网络整理
导读:'last' can stop loop immediately,when you see 'last',the loop is over. ? while(STDIN){ ?if(/__END__/){ ?last; ?}elsif(/fred/){ ?print; ?} } ## last jumps to here ? Whenever input contains line '__END__',the loop ends adn Perl executes the
'last' can stop loop immediately,when you see 'last',the loop is over.
?
while(<STDIN>){
?if(/__END__/){
?last;
?}elsif(/fred/){
?print;
?}
}
## last jumps to here
?
Whenever input contains line '__END__',the loop ends adn Perl executes the rest of codes.
?
'next' immediately stops the current time of loop,and go to the bottom of loop and continue the next time of loop.
?
while(<>){
?foreach(split){?# split $_ into words,give each word to $_
?$total++;
?next if /W/;?# if not word,jumps from the rest of expressions
?$valid++;
?$count{$_}++;?# caculate appears of each words
?## above 'next' jumps to here
?}
}
print "total things = $total,valid words = $validn";
foreach $word(sort keys %count){
?print "$word was seen $count{$word} times.n";
}
?
'redo' redo the current time of time of loop,does not test any condition or go to the next time of loop.
?
my @words=qw{ Fred barney pebbles dino wilma betty };
my $errors=0;
foreach(@words){
?print "Type the word '$_': ";
?chomp(my $try=<STDIN>);
?if($try ne $_){
??print "Sorry - That's not right.nn";
??$errors++;
??redo;
?}
}
print "You've completed the test,with $errors errors.n";
?
foreach(1..10){
?print "Iteration number $_.nn";
?print "Please choose: last,next,redo,or none of the above? ";
?chomp(my $choice=<STDIN>);
?print "n";
?last if $choice =~ /last/i;
?next if $choice =~ /next/i;
?redo if $choice =~ /redo/i;
?print "That wasn't any of the choices... onward!nn";
}
print "That's all,folks!n";
?
Lable can be used for last/next/redo,they'll go to the lable position.
?
LINE: while(<>){
?foreach(split){
??last LINE if /__END__/;?# jump out of loop at LINE
??...
?}
}
?
condition ? true expression : false expression
?
my $location=&is_weekend($day) ? "home" : "work";
my $average=$n?($total/$n):"-----";
print "Average: $averagen";
?
my $size=
?($width<10) ? "small" :
?($width<20) ? "medium" :
?($width<50) ? "large" :
?????"extra-large";?# default
?
&& means "and",|| means "or" in condition
?
if((9<=$hour)&&($hour<17)){
?print "Aren't you supposed to be at work...?n";
}
?
if(($name eq 'fred')||($name eq 'barney')){
?print "You're my kind of guy!n";
}
?
Exercise: Write a program,ask user to input a number between 1 and 100 to match the given randon digit. If high or low tell him about it and retry,if matches or user inputs quit/exit/enter then the program ends.#!/usr/bin/perl$num=int(1 + rand 100);print "Please guese the number between 1 and 100: ";while(<>){??? chomp if $_ ne "n";??? exit if $_==$num || /quit|exit|n/;??? if($_>$num){??? ??? print "Too high,please retype: ";??? }elsif($_<$num){??? ??? print "Too low,please retype: ";??? }??? next;}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读