Why is $#ARGV 0? - perl - Stack Overflow

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

$#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  



請為這篇文章評分?