using Perl I am trying to push the elements of an array to another array, and not the whole array. But I'm not getting my goal.
使用Perl我试图将数组的元素推送到另一个数组,而不是整个数组。但我没有达到我的目标。
I tried this:
我试过这个:
push @tmp_entities_all, @tmp_entities;
But I got the whole small array as an element in the bigger array.
但是我将整个小数组作为更大数组中的元素。
Then I tried it with a loop:
然后我用循环尝试了它:
for (@tmp_entities) {push @tmp_entities_all, $_;}
But the same results, the whole @tmp_entities
appears as an element, and that what I dont want.
但是同样的结果,整个@tmp_entities显示为一个元素,而我不想要的。
I need one dimension in the array and not array of arrays!! Should I cast something before pushing? or what is the problem?
我需要数组中的一个维度而不是数组数组!我应该在推前推出一些东西吗?或者问题是什么?
Thanx a lot.
非常感谢。
1 个解决方案
#1
1
Out of your comments, its obvious, that @tmp_entities
contains only one element, that is an array reference to the elements that you expected to be elements of @tmp_entities
.
显而易见的是,@ tmp_entities只包含一个元素,它是对您希望成为@tmp_entities元素的元素的数组引用。
So you perhaps declared your array with an array refence instead of using a set of elements.
所以你可能用数组refence声明你的数组而不是使用一组元素。
The line
这条线
push @tmp_entities_all, @tmp_entities;
definitly works for a normal array.
绝对适用于普通阵列。
In your case, your could try ...
在你的情况下,你可以试试......
push @tmp_entities_all, $tmp_entities[0];
or you simply try to initialize your array with its value like
或者您只是尝试使用其值来初始化数组
my @tmp_entities = ( 1, 2, 3 ); # initialize array with 3 elements of type int
instead of
代替
my @tmp_entities = [ 1, 2, 3 ]; # initialize array with 1 element that is an array reference with 3 elements of type int
I know, that this is the case, because this is why your for-loop sample works with @$_
;D (it is equivalent to push @tmp_entities_all, $tmp_entities[0];
in this situation).
我知道,情况确实如此,因为这就是你的for循环样本与@ $ _; D一起使用的原因(它相当于推@tmp_entities_all,$ tmp_entities [0];在这种情况下)。
#1
1
Out of your comments, its obvious, that @tmp_entities
contains only one element, that is an array reference to the elements that you expected to be elements of @tmp_entities
.
显而易见的是,@ tmp_entities只包含一个元素,它是对您希望成为@tmp_entities元素的元素的数组引用。
So you perhaps declared your array with an array refence instead of using a set of elements.
所以你可能用数组refence声明你的数组而不是使用一组元素。
The line
这条线
push @tmp_entities_all, @tmp_entities;
definitly works for a normal array.
绝对适用于普通阵列。
In your case, your could try ...
在你的情况下,你可以试试......
push @tmp_entities_all, $tmp_entities[0];
or you simply try to initialize your array with its value like
或者您只是尝试使用其值来初始化数组
my @tmp_entities = ( 1, 2, 3 ); # initialize array with 3 elements of type int
instead of
代替
my @tmp_entities = [ 1, 2, 3 ]; # initialize array with 1 element that is an array reference with 3 elements of type int
I know, that this is the case, because this is why your for-loop sample works with @$_
;D (it is equivalent to push @tmp_entities_all, $tmp_entities[0];
in this situation).
我知道,情况确实如此,因为这就是你的for循环样本与@ $ _; D一起使用的原因(它相当于推@tmp_entities_all,$ tmp_entities [0];在这种情况下)。