Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert multiple values from an array into another array

I need to process a large amount of data in arrays with Perl. At certain points, I will need to insert the values of a second array within a primary array. I have seen that splice should normally be the way to go. However, after having researched a bit, I have seen that this function is memory intensive and over time could cause a serious performance issue.

Here is basically what I am needing to do -

# two arrays
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@second = [a, b, c, d e];

Now insert the content of @second into @primary at offset 4 to obtain -

@primary = [1, 2, 3, 4, a, b, c, d, e, 5, 6, 7, 8, 9];

Would using linked lists be the most efficient way to go when I have to handle a primary array which holds more than 2000 elements ?

Note: can anyone confirm that this is the correct way to do it

$Tail = splice($primary, 4);
push(@primary, @second, $Tail);

?

like image 718
Simon Avatar asked Apr 20 '11 15:04

Simon


1 Answers

splice @primary, 4, 0, @second;
like image 143
Toto Avatar answered Nov 15 '22 04:11

Toto