Perl next Statement

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

The Perl next statement is used inside a loop to start the next iteration and skip all code below it. In practice, you often use the next statement in ... SkiptocontentHome»PerlnextStatementSummary:inthistutorial,youwilllearnhowtousethePerlnextstatementtocontroltheflowoftheloops.IntroductiontoPerlnextstatementThePerlnextstatementisusedinsidealooptostartthenextiterationandskipallcodebelowit.Inpractice,youoftenusethenextstatementinconjunctionwiththeifstatementtospecifyaconditiontostartthenextiteration.Typically,youusethenextstatementinthewhileandforloopstatement.Forexamples.Inawhileloopstatement:while(condition){ nextif(expression); #codetoprocesstheselectedelement }Codelanguage:Perl(perl)Inaforloopstatement:for(@array){ nextif(expression); #codetoprocesstheselectedelement }Codelanguage:Perl(perl)ThenextstatementislikethecontinuestatementinC.ThefollowingflowchartillustrateshowthePerlnextisusedinconjunctionwithanifstatementinsideawhileloop.PerlnextstatementexamplesYouoftenusethenextstatementinthefor,whileanduntilloopstatements.Forthedo…whileanddo…untilstatements,youneedtoplaceablockforthenextstatement.1)PerlnextwiththeforstatementexampleLet’stakealookatthefollowingexample:#!/usr/bin/perl usewarnings; usestrict; my@haystack=(1,4,3,2,5,6,8,7,9); #getanintegerintherange1-9fromcommandline my$needle=0; do{ print"Enteranumbertosearch(1-9):\n"; $needle=int(); }until($needle>=1&&$needle<=9); #findthepositionoftheinputinteger my$pos=-1; formy$i(@haystack){ $pos++; nextif($i!=$needle); print("Foundnumber$needleatposition$pos\n"); }Codelanguage:Perl(perl)Howitworks.First,wedeclaredanarrayofintegersfrom1to9.Second,weaskeduserstoinputanintegerwithintherangetosearchfor.Weusedthedountillooptopromptfortheinputuntiltheinputintegerisintherange.Third,weloopedoverelementsofthe@haystackarraytofindthepositionoftheinputinteger.Insidetheloop,ifthecurrentelementofthearrayisequaltotheinputinteger,wedisplayedamessage.Weusedthenextstatementtostartthenextiterationandskipthecodethatdisplaysthemessage.Thefollowingistheoutputoftheprogramwhenweenternumber3tosearch:Enteranumbertosearch(1-9): 3 Foundnumber3atposition2Codelanguage:Perl(perl)2)PerlnextwithwhileloopexampleInthefollowingexample,theprogramasksyoutoenter5positiveintegersandpushthemtoanarray.Ifyouenteraninvalidnumber,thenextstatementskipsthecodethatincreasesthecounterandpushesthenumbertothearray.Aftertheloop,theprogramdisplaysthecontentofthearraythatcontainsinputnumbers.#!/usr/bin/perl usewarnings; usestrict; useconstantMAX=>5; my@nums=(); my$num=0; my$count=0; print"Enter".MAX."positiveintegers:\n"; while($count); #skipiftheinputnumberisnotthepositiveinteger nextif($num<=0); #pushthepositiveintegertothearray push(@nums,$num); $count++; } print("Youentered:@nums\n");Codelanguage:Perl(perl)Theoutputoftheprogram:Enter5positiveintegers: -1 1 0 2 3 7 0 1 Youentered:12371Formoreinformationonusingthenextstatementwithdowhileanddountilstatements,checkitouttherespectivetutorial.Inthistutorial,you’velearnedhơtousethenextstatementtostartthenextiterationoftheloop.Wasthistutorialhelpful?YesNoPreviouslyPerldo…untilStatementUpNextPerllastStatementSearchfor:GettingStartedBasicPerlTutorialPerlI/O



請為這篇文章評分?