Why is $#ARGV 0? - perl - Stack Overflow
文章推薦指數: 80 %
$#ARGV is the count of the array elements so the count of @ARGV here. It is 0 as arrays start at 0 in perl. It would return -1 should you not ... 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 Whyis$#ARGV0? AskQuestion Asked 9years,3monthsago Modified 9years,3monthsago Viewed 6ktimes 5 New!Savequestionsoranswersandorganizeyourfavoritecontent.Learnmore. I'vebeensearchingthroughPerlresources,andIcouldnotseewhereIamgoingwrong.IamprettysureI'mmissingsomethingobvious,becausewhenIassign my$gArgc=$#ARGV; butcallmyprogramperlpkTkPtBdTkNo.pltest.txt $#ARGVisequalto0,andIcan'tfigureoutwhy. #!/usr/bin/perl-w usestrict; my$gArgc=$#ARGV; my$input_line; my$bad_input; print($gArgc); die("UsagepkTkPtBdTkNo.plinput-line") if(0==$gArgc); $input_line=$ARGV[0]; $bad_input=($input_line=~/\"\d+\D+\d*\",/); print($bad_input); perl Share Follow askedJul9,2013at23:04 octopusgrabbusoctopusgrabbus 10.4k1414goldbadges6767silverbadges127127bronzebadges 2 $#ARGVisthecountofthearrayelementssothecountof@ARGVhere.Itis0asarraysstartat0inperl.Itwouldreturn-1shouldyounotpassanyargumentsintoyourscript – KeepCalmAndCarryOn Jul9,2013at23:07 7 Justtobepedantic,it'snotthecountofelementsin@ARGV,buttheindexofthelastelement,whichiswhyit's0.Youshouldputyouranswerinsoitcanbeupvoted(aftertakingthisintoaccount). – JimStewart Jul9,2013at23:13 Addacomment | 4Answers 4 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 10 Frommanperlintro: Thespecialvariable$#arraytellsyoutheindexofthelastelementofanarray: ... Youmightbetemptedtouse$#array+1totellyouhowmanyitemsthereareinanarray. Don'tbother.Asithappens,using@arraywherePerlexpectstofindascalarvalue("in scalarcontext")willgiveyouthenumberofelementsinthearray: So,ifyoupass0arguments,$#ARGVwillbe-1,sincetherearenoelementsinthearray.Ifyoupass1argument(asinyourexample),$#ARGVwillbe0. Share Follow editedJul10,2013at2:41 ikegami 352k1515goldbadges254254silverbadges499499bronzebadges answeredJul9,2013at23:24 atkatk 9,15433goldbadges3131silverbadges3232bronzebadges 0 Addacomment | 2 Thisshouldbealwaystrue,$#ARGV+1==@ARGVas$#ARGVislastindexof@ARGVarray. Share Follow answeredJul9,2013at23:09 mpapecmpapec 49.8k88goldbadges6464silverbadges122122bronzebadges Addacomment | 1 Thevariable$#ARGVisthesubscriptofthelastelementofthe@ARGVarray,andbecausethearrayiszero-based,thenumberofargumentsgivenonthecommandlineis$#ARGV+1. Share Follow answeredJul10,2013at6:30 pkmpkm 2,62511goldbadge2727silverbadges4444bronzebadges Addacomment | 0 Youcouldtry: @ARGV ordie"UsagepkTkPtBdTkNo.plinput-line" Ifindthatavoidingthe$#array_namesyntaxentirelyimprovesreadabilitygreatly, sincehereperlwillturn@ARGVintoascalar(withvalue0)automatically. Share Follow answeredJul10,2013at6:49 xxfelixxxxxfelixxx 6,39833goldbadges3030silverbadges3838bronzebadges Addacomment | YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedperloraskyourownquestion. TheOverflowBlog IntroducingtheOverflowOfflineproject Hehelpedbuild.NETandVSCode—Now’sheworkingonWeb3(Ep.499) FeaturedonMeta The2022Community-a-thonhasbegun! Mobileappinfrastructurebeingdecommissioned Visitchat Related 1 ProblemswhilemakingamultiprocessingtaskinPerl 0 InstallingPerlModuleinApacheTomcatServer 520 Whyisthisprogramvalid?Iwastryingtocreateasyntaxerror 3 VariableinaPerlfunctionhasanunexpectedvalueoutsidethefunction 5 Perl:openor[blockofcode]? 0 Perlerror:Can'[email protected] 2 Printeachlineofafile 1 MyPerlcan'tfindlocal::libafterupgradingfromPerl5.14to5.28 HotNetworkQuestions WhyareUKprimeministersresigning,ratherthanwaitingouttheirtermandenjoyingthebenefits? IsGoogleStreetViewcoverageinGermanyexpectedtoincrease? IsthereaVFRmobileappthatshowsotheraircraftinrealtime? WhereisthisparkinMontreal,withautumncolortreesandpond? WhyareringsnotafavoriteoptioninITnetworks? DoAustraliancitizensneedaletterofinvitationtovisitaSchengenandEUcountryfortourism? HowdoIcreategeometry/texturesforproceduralshading? Whichoneisbetter"hadbrokendown"or"hadbeenbrokendown"? EquilibriumconstantvsReactionrateconstant WouldaTrump-nominatedSupremeCourtjusticebeexpectedtorecusethemselvesfromaTrumpcase? HowcanIprintoutthefirstlineofallthefilesthatmatchcertain"find"criteria? 1v1barteringbraingame– seeanyproblems? Whataredisadvantagesofaerodropbars? Whattodoafterapuremathacademicpath? AlphabetChecksum Zeropaddingversuszerostuffing QGISdelimitedtextlayerisnotvisible ShouldItellmyadvisormyfianceleftme? Idealprogramminglanguageforanoperationsresearcher Whatisthemostefficientwaytoget110Vfrom220Vsingle-phasewiring? Whyarefangsofgirlssocommoninanime? Wouldlanguagehavemeaningiftherewasnoconsciousness? HowcanApophishaveason? Selectrankofsinglerow morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-perl Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1即時餵資料給程式吃 - 朝陽科技大學
Perl 會把使用者敲在命令列上的參數放在@ARGV 陣列裡。 (熟悉C 語言的讀者請注意: 在perl 當中, $ARGV[0] 是第一個參數, 不是命令名稱) 程式執行方式(以count_a...
- 2[Perl 筆記] 預設變數@ARGV - 叮咚- 痞客邦
學習記錄﹞- Perl – 預設變數預設變數: $_ , @_, @ARGV @ARGV: 存放命令列引數的陣列$#ARGV: 陣列最後一個索引值執行程式時後面.
- 3Why is $#ARGV 0? - perl - Stack Overflow
$#ARGV is the count of the array elements so the count of @ARGV here. It is 0 as arrays start at ...
- 4Perl的命令行参数和ARGV - 骏马金龙- 博客园
perl将perl命令行的参数列表放进数组ARGV(@ARGV)中。既然是数组,就可以访问($ARGV[n])、遍历,甚至修改数组元素; ARGV数组分三种情况收集:.
- 5Processing command line arguments - @ARGV in Perl
Perl automatically provides an array called @ARGV, that holds all the values from the command lin...