How do I break out of a loop in Perl? - Stack Overflow
文章推薦指數: 80 %
Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154. Is there a workaround for this (besides disabling strict subs)?. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam StackOverflowforTeamsismovingtoitsowndomain!Whenthemigrationiscomplete,youwillaccessyourTeamsatstackoverflowteams.com,andtheywillnolongerappearintheleftsidebaronstackoverflow.com. Checkyouremailforupdates. Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams HowdoIbreakoutofaloopinPerl? AskQuestion Asked 13years,11monthsago Modified 2years,1monthago Viewed 596ktimes 317 New!Savequestionsoranswersandorganizeyourfavoritecontent.Learnmore. I'mtryingtouseabreakstatementinaforloop,butsinceI'malsousingstrictsubsinmyPerlcode,I'mgettinganerrorsaying: Bareword"break"notallowedwhile "strictsubs"inuseat./final.pl line154. Isthereaworkaroundforthis(besidesdisablingstrictsubs)? Mycodeisformattedasfollows: formy$entry(@array){ if($stringeq"text"){ break; } } perlloopsbreakstrict Share Improvethisquestion Follow editedMar16,2014at20:27 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges askedNov19,2008at20:21 ZainRizviZainRizvi 23.1k2020goldbadges8888silverbadges127127bronzebadges 1 24 Andifyoudidn'thave"strictsubs"on,youwouldhavegottenarun-timeerrorinsteadwhenitcouldn'tfindasubnamed"break". – PaulTomblin Nov19,2008at20:43 Addacomment | 5Answers 5 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 467 Oh,Ifoundit.Youuselastinsteadofbreak formy$entry(@array){ if($stringeq"text"){ last; } } Share Improvethisanswer Follow editedJan5,2018at17:41 AlexanderRoskamp 4766bronzebadges answeredNov19,2008at20:23 ZainRizviZainRizvi 23.1k2020goldbadges8888silverbadges127127bronzebadges 4 Thoughthisismerelyasemanticdifferenceanddoesn'tamounttomuchelseotherthantextualconsistencywiththe"next"command. – RazorStorm Jul8,2010at20:14 1 nottomentionthat'break'isanexperimentalkeywordin>5.010right?soyoumightgetanillegaloutsidegivenblockerror,notthatyoushoulduseit.Theonlywaythatwouldevenworkisifyouusedawhileloopinsideagivenblock,andthenbreak,whichintheorywouldworkthewayyouwantedIF(andonlyif)thegivenblockonlyhadthewhileloopinit,andassumingyouwerejustfeedinggivensomescalarvariable...butwhydoallthatworkjusttouseabreak,itis(very)badform,nottomention(blah!)justsaying,its"possible"justnotagoodidea¬whatyoumaythinkitis – osirisgothra Sep1,2014at15:50 30 forsomereason,iamcompletelyunabletorememberthiskeyword'last'.alwaysendupgoogling'perlbreakloop':( – Thupten Sep8,2015at4:50 Also,worksthesameforwhile()loops.my@array=("_","apple","orange");my$thing;while($thing=shift@array){ lastif$thing=~/[A-Za-z]/;}print($thing); #"apple" – HoldOffHunger Jul17,2018at19:06 Addacomment | 177 Additionaldata(incaseyouhavemorequestions): FOO:{ formy$i(@listone){ formy$j(@listtwo){ if(cond($i,$j)){ lastFOO;#---> #| }#| }#| }#| }#
延伸文章資訊
- 1how to break out of a loop in Perl | alvinalexander.com
In many programming languages you use the break operator to break out of a loop like this, but in...
- 2How 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...
- 3Perl的基本語法
Perl也有和C語言的break和continue一樣的指令,Perl叫它做last 和next (較口語化)。 ... 下面是一個十分精簡的寫法,和while($_=<FILE>){print...
- 4Perl do while - Perl Tutorial
The last statement exits the do...while loop immediately. It acts as the break statement in C/C++...
- 5How do I break out of a loop in Perl? - Stack Overflow
Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154. Is there a workar...