shift in Perl - Perl Maven

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

The shift function in Perl will remove the first value of the array passed to it and return it. ... Note, the array itself got shorter. The first element was ... Togglenavigation PerlMaven PerlTutorial Pro Login Register Books Typekeyword: Archive About shiftinPerl shift @ARGV @_ TheshiftfunctioninPerlwillremovethefirstvalueofthearraypassedtoitandreturnit. my@names=('Foo','Bar','Moo'); my$first=shift@names; print"$first\n";#Foo print"@names\n";#BarMoo Note,thearrayitselfgotshorter.Thefirstelementwasremovedfromit! shiftwithoutparameters Incasenoarrayispassedtoit,shifthastwodefaultsdependingonthelocationofshift. Ifshiftisoutsideanyfunctionittakesthefirstelementof@ARGV(theparameterlistoftheprogram). examples/shift_argv.pl usestrict; usewarnings; my$first=shift; print"$first\n"; $perlexamples/shift_argv.plonetwo one Ifshiftisinsideafunctionittakesthefirstelementof@_(theparameterlistofthefunction). examples/shift_in_sub.pl usestrict; usewarnings; subsomething{ my$first=shift; print"$first\n"; } something('hello','world'); $perlexamples/shift_in_sub.plonetwo hello Hereshiftdisregardedthecontentof@ARGVandtookthefirstelementof@_,thearray holdingtheparameterspassedtothefunction. Shiftonemptyarray Ifthearrayshiftworksonisempty,thenshiftwillreturnundef. Regardlessifthearraywasexplicitelygiventoitorimplicelyselectedbasedonthelocationofshift. Seevideoexplainingshiftandunshift ortheexampleusingshifttogetfirstparameterofascript. Writtenby GaborSzabo Publishedon2015-12-17 Comments Inthecomments,pleasewrapyourcodesnippetswithin

tagsandusespacesforindentation.
commentspoweredbyDisqus
Ifyouhaveanycommentsorquestions,feelfreetopostthemonthesourceofthispageinGitHub.SourceonGitHub.
Commentonthispost
Author:GaborSzabo
GaborwhorunsthePerlMavensitehelpscompaniessetuptestautomation,CI/CD
ContinuousIntegrationandContinuousDeploymentandotherDevOpsrelated
systems.
GaborcanhelprefactoryouroldPerlcode-base.
HerunsthePerlWeeklynewsletter.
ContactGaborifyou'dliketohirehisservice.
BuyhiseBooksorifyoujustwouldliketosupporthim,doitviaPatreon.



請為這篇文章評分?