How to break out of a loop in Perl - Educative.io

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

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.



請為這篇文章評分?