Perl last Statement
文章推薦指數: 80 %
The Perl last statement is used inside a loop to exit the loop immediately. The last statement is like the break statement in other languages such as C/C++, ...
SkiptocontentHome»PerllastStatementSummary:inthistutorial,youwilllearnhowtousethePerllaststatementtoexitaloopimmediately.IntroductiontothePerllaststatementThePerllaststatementisusedinsidealooptoexittheloopimmediately.ThelaststatementislikethebreakstatementinotherlanguagessuchasC/C++,Java.Inpractice,youoftenusethelaststatementtoexitaloopifaconditionissatisfiede.g.,youfindanarrayelementthatmatchesasearchkey,therefore,itisnotnecessarytoexamineotherelements.Becauseofthis,youtypicallyusethelaststatementinconjunctionwithaconditionalstatementsuchasiforunless.Thefollowingillustrateshowtousethelaststatementinsideawhileorforloopstatement.Insideawhilestatement(thesamefortheuntilstatement):while(condition){
#processsomething
lastif(expression);
#somemorecode
}Codelanguage:Perl(perl)Insideaforloopstatement:for(@a){
#processcurrentelement
lastif(condition);
#somemorecode
}Codelanguage:Perl(perl)Forthedowhileanddountilstatement,youhavetouseanadditionalblock{}asdescribedinmoredetailinthecorrespondingtutorial.Thefollowingflowchartillustrateshowthelaststatementworksinsideawhileloopstatement.PerllaststatementexamplesLet’sstartwithasimpleexampletofindanelementinahashbyagivenkey.#!/usr/bin/perl
usewarnings;
usestrict;
my($key,$value);
my%h=("apple"=>1,
"orange"=>2,
"mango"=>3,
"coconut"=>4);
print("Pleaseenterakeytosearch:\n");
$key=
延伸文章資訊
- 1精簡扼要的Perl 課程講義(三):迴圈與控制結構 - G. T. Wang
next 跳到這裡 } # last 跳到這裡繼續執行 # 註:next, last 與redo 皆可用在for,foreach,while 與until 等迴圈。 # (7) if # (A)...
- 2What does "last" do in Perl? - Stack Overflow
Available uses for last command: ... According to Perl's documentation: The last command is like ...
- 3Perl last Statement
The Perl last statement is used inside a loop to exit the loop immediately. The last statement is...
- 4last - Perldoc Browser
If the LABEL is omitted, the command refers to the innermost enclosing loop. The last EXPR form, ...
- 55.7 迴圈控制
Perl 常見的迴圈有while、for、foreach、until,在每一種迴圈中,可以視情況,使用以下三種算符,來控制迴圈。 last 一次只能跳出一層迴圈語法:last 用例: $i=0;...