Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert an array into an array of arrays?

Tags:

raku

How could I insert an array into an array of arrays?

In Perl 5 I would do it this way:

use Data::Dumper;
my @aoa = ( [ 'a', 'A' ], [ 'c', 'C' ] );
splice( @aoa, 1, 0, [ 'b', 'B' ] );
print Dumper \@aoa;

(In Perl 6 splice flattens the replacement)

like image 850
sid_com Avatar asked Mar 22 '19 18:03

sid_com


People also ask

How do you add an array to an array of arrays?

To append one array to another, call the concat() method on the first array, passing it the second array as a parameter, e.g. const arr3 = arr1. concat(arr2) . The concat method will merge the two arrays and will return a new array.

Can I put an array in an array JavaScript?

Introduction to JavaScript multidimensional arrayJavaScript does not provide the multidimensional array natively. However, you can create a multidimensional array by defining an array of elements, where each element is also another array.

How to insert an element from an array of elements?

1 First get the element to be inserted, say x 2 Then get the position at which this element is to be inserted, say pos 3 Then shift the array elements from this position to one position forward, and do this for all the other elements next to... 4 Insert the element x now at the position pos, as this is now empty. More ...

How to add an array to array at the beginning in JavaScript?

To add an array to array at the beginning in JavaScript, use the array.unshift () method. The unshift () is a built-in JavaScript function that adds new elements to the beginning of an array and returns the new length. The elem1, elem2, …, elemX arguments are required. The element (s) to add to the beginning of the array.

How to insert another array into an array without creating one?

If you want to insert another array into an array without creating a new one, the easiest way is to use either push or unshift with apply. Eg: a1 = [1,2,3,4,5]; a2 = [21,22]; // Insert a1 at beginning of a2 a2.unshift.apply (a2,a1); // Insert a1 at end of a2 a2.push.apply (a2,a1);

How do I add multiple elements to an array in JavaScript?

You can also add multiple elements by using multiple parameters in the push () method: The unshift () method inserts elements to the beginning of the array. The concat () method doesn’t actually append elements to an existing array but instead, creates a brand new array. This means that the original array won’t change. Why is this important?


1 Answers

my @aoa = ([1,2],[5,6]);
my @arr = 3,4;
splice(@aoa, 1, 0, [@arr,]); # or splice(@aoa, 1, 0, [[3, 4],]);
say @aoa.perl
like image 110
ugexe Avatar answered Oct 13 '22 19:10

ugexe