Good day. As with Perl, you can insert elements of one array into another after a certain number of elements.

There is an array

@DATA=[1a,2b,3c,4d,5f,6g,7q,8r,...235bgh,1b,2g,3k,4j...]; 

That is, I do not know what exactly is contained in rows, in an array of more than 1000k rows. I need to add array elements to this array in turn:

 @DATA2=[xxx,yyy,zzz,...]; 

In which the content of the strings is also not known, after every 235 line of the @DATA array

Using splice adds all the elements immediately after you need a line (basically, as it should be)

 open (IN,"<$xxx"); open (OUT, "<$zzz"); my @DATA=<OUT>; my @DATA2=<IN>; splice @DATA, 234,0,@DATA2; print dump(\@DATA); close IN; close OUT; 

Can there be other ways to do this?

  • Sorry, I understood the task a little first. Then the question is: what to do if the elements of the first array are over, but still remain in the second? - PinkTux 4:34
  • They cannot end, each 235 elements of the first array corresponds to 1 element of the second. - Evgeniy A
  • Can. If, for example, in the first array 470 elements, and in the second - 10. - PinkTux

1 answer 1

If modification of the original array is needed:

 insert_data( \@source_array, \@add_from, 235 ); # ... sub insert_data { my ( $src, $add, $portion ) = @_; for ( my $i = 0; $i < @{$add}; $i++ ) { splice @{$src}, $portion * ( $i + 1 ) + $i, 0, $add->[$i]; } return $src; }