Perl next operator - for loop and if statement examples
文章推薦指數: 80 %
When you're in a Perl for loop (iterating over an array or hash), and you want to move on to the next element in your array or hash, just use ... 2021cookbook #1newrelease! Perlnextoperator-forloopandifstatementexamples ByAlvinAlexander.Lastupdated:June4,2016 PerlnextloopFAQ: CanyoudemonstratehowtousethePerlnextoperatorinaforloop? Problem:You'rewritingcodeforaPerlloop,andyouneedtowritesomelogictoskipoverthecurrentelementintheloop,andmoveontothenextloopelement. ThePerlloopnextoperator Whenyou'reinaPerlforloop(iteratingoveranarrayorhash),andyouwanttomoveontothenextelementinyourarrayorhash,justusethePerlnextoperator,likethis: for(@array) { if(SOME_TEST_CONDITION) { #moveontothenextloopelement next; } #morecodehere... } MorePerlnextexamples YoucanseesomedecentexamplesofthePerlnextoperatorinthefollowingcode.InthecodejustbeforethisforloopIhadjustreadalltherecordsofaPerlscriptintoanarraynamed@records,andinthePerlforloopshownbelowI'mskippingoverallthestringsI'mnotreallyinterestedin: for(@records) { #skipblanklines nextif/^[\t]*$/; #skipcommentlines nextif/#/; #skipprintinglines nextif/print/; nextif/printf/; #muchmorecodehere... } Perlnextoperator-Summary Asyoucansee,youusethePerlnextoperatortoskiptothenextelementinaPerlforloop,andthereareatleasttwowaystousethenextoperator,asshownintheexamplesabove:insideanifstatement,orbeforeanifstatement. Related-howtobreakoutofaPerlforloop Inarelatednote,youcanbreakoutofaPerlforloopusingthePerllastoperator,likethis: if(SOME_TEST_CONDITION) { last; } ThePerllastoperatorworksjustlikethebreakoperatorinotherlanguages,breakingoutoftheloopwhenthelastoperatorisencountered.YoucanreadmoreaboutthelastoperatorinmyPerlforloopbreaktutorial. perl else forloop if next perl perlloop last continue Perlloop-howtobreakoutofaloopinPerl Perlcomparisonoperators Perl‘equals’FAQ:WhatistrueandfalseinPerl? Perlif,else,elsif("elseif")syntax HowtousethePerlternaryoperator booksi’vewritten (Almost)FunctionalProgramming SomeofShinzenYoung’ssayingsinthefirstcorelessonsoftheBrightmindapp LearnScala3TheFastWay!(BestwaytolearnScala!) FunctionalProgramming,Simplified(aScalabook) HomagetoHaskellandFunctionalProgramming
延伸文章資訊
- 1Perl continue 語句 - W3big
continue 語句可用在while 和foreach 循環中。 語法. while 循環中continue 語句語法格式如下所示: while(condition){ statement(s...
- 2Perl continue Statement - Tutorialspoint
A continue BLOCK, is always executed just before the conditional is about to be evaluated again. ...
- 35.7 迴圈控制
Perl 常見的迴圈有while、for、foreach、until,在每一種迴圈中,可以視情況, ... 至最外圈。 print "$i\n"; } print "我是j : $j\n"; }...
- 4Loop controls: next, last, continue, break - Perl Maven
In Perl there are 3 loop control keywords. The two commonly used are next and last and there is a...
- 5Perl next operator - for loop and if statement examples
When you're in a Perl for loop (iterating over an array or hash), and you want to move on to the ...