Appending One Array to Another - Perl Cookbook [Book]

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

Appending One Array to Another Problem You want to join two arrays by appending all the elements of one to the end of the other. Solution Use push: # … Skiptomaincontent PerlCookbookby GetfullaccesstoPerlCookbookand60K+othertitles,withfree10-daytrialofO'Reilly. There'salsoliveonlineevents,interactivecontent,certificationprepmaterials,andmore. Startyourfreetrial AppendingOneArraytoAnotherProblem Youwanttojointwoarraysbyappendingalltheelementsofoneto theendoftheother.SolutionUsepush:#push push(@ARRAY1,@ARRAY2);DiscussionThepushfunctionisoptimizedforappendinga listtotheendofanarray.YoucantakeadvantageofPerl’s listflatteningtojointwoarrays,butitresultsinsignificantly morecopyingthanpush:@ARRAY1=(@ARRAY1,@ARRAY2);Here’sanexampleofpushinaction:@members=("Time","Flies"); @initiates=("An","Arrow"); push(@members,@initiates); #@membersisnow("Time","Flies","An","Arrow")Ifyouwanttoinserttheelementsofonearrayintothemiddleof another,usethesplicefunction:splice(@members,2,0,"Like",@initiates); print"@members\n"; splice(@members,0,1,"Fruit"); splice(@members,-2,2,"A","Banana"); print"@members\n";Thisisoutput: TimeFliesLikeAnArrow FruitFliesLikeABananaSeeAlsoThespliceandpushfunctions inperlfunc(1)andChapter3of ProgrammingPerl;the“ListValuesand Arrays”sectionofChapter2ofProgramming Perl;the“ListValueConstructors”section ofperldata(1) GetPerlCookbooknowwiththeO’Reillylearningplatform. O’Reillymembersexperienceliveonlinetraining,plusbooks,videos,anddigitalcontentfromnearly200publishers. Startyourfreetrial Don’tleaveempty-handed GetMarkRichards’sSoftwareArchitecturePatternsebooktobetterunderstandhowtodesigncomponents—andhowtheyshouldinteract. It’syours,free. Getitnow Close



請為這篇文章評分?