Perl last Statement

文章推薦指數: 80 %
投票人數:10人

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=; chomp($key); $value=0; #searching foreach(keys%h){ if($_eq$key){ $value=$h{$_}; last;#stopsearchingiffound } } #printtheresult if($value>0){ print("element$keyfoundwithvalue:$value\n"); }else{ print("element$keynotfound\n"); }Codelanguage:Perl(perl)Howprogramworks.First,weloopedovertheelementsofthehashandcomparedeachhashkeywiththesearchkey.Second,insidetheloop,ifwefoundthematch,weexitedtheloopimmediatelybyusingthelaststatementwithoutexaminingotherelements.Third,wedisplayedtheresultofthesearch.PerllaststatementwithlooplabelexampleIfyouusethelastcommandalone,itexitsonlytheinnermostloop.Incase,youhaveanestedloopthatisaloop,calledinnerloop,nestedinsideanotherloop,calledouterloop.Ifyouwanttoexitboththeinnerandouterloopyouneedto:Putalabelintheouterloop.Passthelabeltothelaststatement.Thefollowingexampledemonstrateshowtousethe last statementwithalabel:#!/usr/bin/perl usewarnings; usestrict; my($key,$value); my%h=("apple"=>1, "orange"=>2, "mango"=>3, "coconut"=>4); $value=0; print("Pleaseenterakeytosearch:\n"); OUTER:while(){ #getinputformuser $key=$_; chomp($key); #seaching INNER:foreach(keys%h){ if($_eq$key){ $value=$h{$_}; lastOUTER;#exitthewhileloop } }#endfor print("Notfound,Pleasetryagain:\n")if($value==0); }#endwhile print("elementfoundwithvalue:$value\n");Codelanguage:Perl(perl)Inthistutorial,wehaveshownyouhowtoexittheloopsimmediatelybyusingthePerllaststatement.Wasthistutorialhelpful?YesNoPreviouslyPerlnextStatementUpNextPerlRegularExpressionSearchfor:GettingStartedBasicPerlTutorialPerlI/O



請為這篇文章評分?