Appending One Array to Another - Perl Cookbook [Book]
文章推薦指數: 80 %
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
延伸文章資訊
- 1How do I append an element to a perl array? - Dave's Brain
Date: 2014nov11 Language: perl Q. How do I append an element to a perl array? A. Use push() like ...
- 2Perl - Arrays - Tutorialspoint
Adding and Removing Elements in Array. Perl provides a number of useful functions to add and remo...
- 3Perl | Arrays (push, pop, shift, unshift) - GeeksforGeeks
Perl provides various inbuilt functions to add and remove the elements in an array. ... This func...
- 4Arrays - Learn Perl - Free Interactive Perl Tutorial
Perl array variables store an ordered list of scalar values. ... push(@array, element) : add elem...
- 5Manipulating Perl arrays: shift, unshift, push, pop - Perl Maven
The push function can add one or more values to the end of an array. (Well, it can also add 0 val...