我怎样在一个数组引用添加到Perl中的现有阵列的中间? [英] How do I add an array ref to the middle of an existing array in Perl?
本文介绍了我怎样在一个数组引用添加到Perl中的现有阵列的中间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
欲能够阵列放置到数组。例如,我可能有一个这样的数组:
I want to be able to place an array into an array. For example, I may have an array like this:
my @array1 = ("element 1","element 2","element 3");
然后我有另一个数组
Then I have another array
my $array_ref = ["this will", "go between", "element 1 and 2"];
欲放置 $ ARRAY_REF
到第一,使得第一阵列
看起来是这样的:
I want to place $array_ref
into the first so that the first array
looks like this:
("element 1",["this will", "go between", "element 1 and 2"],"element 2","element 3")
我似乎无法做到这一点。我看了看遍谷歌和什么也没找到。
I can't seem to do this. I looked all over Google and found nothing.
推荐答案
所以,你使用剪接以取代0你所需元素与元素1(第二个元素,第一个元素是0)开头的元素:
So you use splice to replace 0 elements beginning with element 1 (the second element, the first is element 0) with your desired elements:
splice( @array, 1, 0, ["this will", "go between", "element 1 and 2"] );
或者可能你的意思是:
Or possibly you mean:
splice( @array, 1, 0, "this will", "go between", "element 1 and 2" );
如果你不想嵌套数组。
这篇关于我怎样在一个数组引用添加到Perl中的现有阵列的中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文