How to break out of a loop in Perl - Educative.io
文章推薦指數: 80 %
The last keyword will allow us to break out of the current enclosing loop. ... If we specify LABEL_NAME , we can break out of the entire label containing the loop ... SolutionsEducativeEnterpriseEnablementplatformDevelopersLearnnewtechnologiesProductsCoursesforEnterpriseSuperchargeyourengineeringteamCoursesforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsAnswersTrustedAnswerstoDeveloperQuestionsPricingForEnterpriseTailoredforyourteamForIndividualsStayaheadofthecurveCoursesLogInJoin forfreeTrustedanswerstodeveloperquestionsTrustedAnswerstoDeveloperQuestionsRelatedTagsperlcommunitycreatorHowtobreakoutofaloopinPerlGuthaVamsiKrishnaGrokkingModernSystemDesignInterviewforEngineers&ManagersAceyourSystemDesignInterviewandtakeyourcareertothenextlevel.LearntohandlethedesignofapplicationslikeNetflix,Quora,Facebook,Uber,andmanymoreina45-mininterview.LearntheRESHADEDframeworkforarchitectingweb-scaleapplicationsbydeterminingrequirements,constraints,andassumptionsbeforedivingintoastep-by-stepdesignprocess. StartLearningOverviewAloopisasequenceofinstructionsthatarecontinuouslyexecuteduntilcertainconditionsaremet.Insomecases,wemaywanttobreaktheloopbeforethegivenconditionsaremet.InPerl,wehavethelastkeywordtobreakoutoftheloop.SyntaxlastThelastkeywordwillallowustobreakoutofthecurrentenclosingloop.lastLABEL_NAMEIfwespecifyLABEL_NAME,wecanbreakoutoftheentirelabelcontainingtheloop(s).Thisisusuallyusedinnestedloops.Example1Wewillloopthroughanarray,ages,andbreakoutoftheloopwhenacertainconditionismet.#arrayofages@ages=(25,60,40,10,30,55,76,34);#forloopformy$age(@ages){if($age==30){#breakthelooplast;}print"$age\n";}RunExplanationLine2:Wedeclareandinitializeanarray,@ages.Line5:Weuseaforlooptotraverseeachelementinthearray,@ages.Line6:Wecheckifthecurrentelementinthearray,$age,isequalto30.Line8:Iftheconditionaboveismet,webreakoutoftheloopusingthelastkeyword.Line10:Otherwise,weprintthecurrentelementofthearray,ages.Example2Wewillloopthroughthearray,@scores1.Withinthisloop,wewillloopthroughanotherarray,@scores2.WewillbreakoutofboththeloopswithinLABELwhenacertainconditionismet.@scores1=(25,60,40,10,30,55,76,34);@scores2=(24,59,39,10,29,54,75,33);SCORES:{formy$elem1(@scores1){formy$elem2(@scores2){if($elem1eq$elem2){print"$elem1";lastSCORES;}}}}RunExplanationLines1–2:Wedeclareandinitializetwoarrays,@scores1,[email protected]:Wedefinedalabel,SCORES.Line5:[email protected]:[email protected]:Withinthisnestedloop,wecheckwhetherthecurrentelement,$elem1,ofthe@scores1arrayisequaltothecurrentelement,$elem2,[email protected]–9:Iftheconditionaboveismet,weprinttheelement,$elem1of@[email protected],webreakoutofboththeloopswithinthelabel,SCORES,byusinglastSCORES.Otherwise,wedonothing.RELATEDTAGSperlcommunitycreatorCONTRIBUTORGuthaVamsiKrishnaLicense:CreativeCommons-Attribution-ShareAlike4.0(CC-BY-SA4.0)GrokkingModernSystemDesignInterviewforEngineers&ManagersAceyourSystemDesignInterviewandtakeyourcareertothenextlevel.LearntohandlethedesignofapplicationslikeNetflix,Quora,Facebook,Uber,andmanymoreina45-mininterview.LearntheRESHADEDframeworkforarchitectingweb-scaleapplicationsbydeterminingrequirements,constraints,andassumptionsbeforedivingintoastep-by-stepdesignprocess. StartLearningKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSForEnterpriseForIndividualsForHR&RecruitingForBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsEducativeProjectsPricingForEnterpriseForIndividualsFreeTrialLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorRESOURCESEducativeBlogEMHubEducativeSessionsEducativeAnswersABOUTUSOurTeamCareersHiringFrequentlyAskedQuestionsContactUsPressMOREGitHubStudentsScholarshipCourseCatalogEarlyAccessCoursesEarnReferralCreditsCodingInterview.comCopyright©2022Educative,Inc.Allrightsreserved.
延伸文章資訊
- 1Perl do while - Perl Tutorial
The last statement exits the do...while loop immediately. It acts as the break statement in C/C++...
- 2Perl last Statement - javatpoint
Using the Perl last statement alone, you can exits only innermost loop. If you want to exit a nes...
- 3How to break out of a loop in Perl - Educative.io
The last keyword will allow us to break out of the current enclosing loop. ... If we specify LABE...
- 4[PERL] 09-迴圈@新精讚
perl的迴圈. ... 迴圈和一般程式語言大同小異,主要有幾種. while. do...while. do...until. foreach ... last 這是最後一筆,等同於其它語言的...
- 5Loop 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...