如何获取数组中存在的子数组的值
我已经创建了一个数组,并将每个迭代的循环中的另一个数组推入其中。完成后,我想获取每个子数组和数组的值。
我使用的代码:
my $i;
my @carrierValuesAll="";
while (my @row = $sth->fetchrow_array()) {
my @carrierValues=join(',', @row). "\n";
push(@carrierValuesAll, @carrierValues);
}
$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
print("\n");
}
我获得的输出是:
@carrierValuesAll[1] = 1,1,https://au-sbc.trustidinc.com/tid,sbcLabStub,sbcLab,,SKY,0,2019-11-07 20:10:43,2021-07-02 04:39:43,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,trustid
@carrierValuesAll[2] = 2,1,https://au-sbc.trustidinc.com/tid,sbcLab,sbcLab,,SKY,2,2019-11-07 20:10:43,2020-12-14 06:24:17,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,admin
我尝试过的内容:
$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
print("\n");
for (my $j=0;$j<$carrierValuesAll[$i];$j++) {
print("\@carrierValuesAll[$j] = $carrierValuesAll[$j]\n");
print("\n");
}
}
在其中添加了一个嵌套循环,但它不会有帮助。
此输出是数据库&amp;我正在尝试将其存储在数组中。
I have created one array and pushing another array in each iteration of loop into it. Once it will be done I wanted to fetch the values of each sub array as well as array.
Code I am using :
my $i;
my @carrierValuesAll="";
while (my @row = $sth->fetchrow_array()) {
my @carrierValues=join(',', @row). "\n";
push(@carrierValuesAll, @carrierValues);
}
$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
print("\n");
}
The output I am getting is :
@carrierValuesAll[1] = 1,1,https://au-sbc.trustidinc.com/tid,sbcLabStub,sbcLab,,SKY,0,2019-11-07 20:10:43,2021-07-02 04:39:43,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,trustid
@carrierValuesAll[2] = 2,1,https://au-sbc.trustidinc.com/tid,sbcLab,sbcLab,,SKY,2,2019-11-07 20:10:43,2020-12-14 06:24:17,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,admin
What I have tried :
$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
print("\n");
for (my $j=0;$j<$carrierValuesAll[$i];$j++) {
print("\@carrierValuesAll[$j] = $carrierValuesAll[$j]\n");
print("\n");
}
}
added one more nested loop into it, but it wont helped.
This output is what coming from database & I am trying to store it in array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里只有一个数组 - 嗯,您有第二个数组 variable ,但是您只在其中存储一个标量值。
让我们看一下您的代码:
声明
$ i
距使用的地方很遥远,这有点像代码气味。当然,它可以正常工作,但是Perl使您可以灵活地声明使用它们的变量 - 因此请使用它。@CarrierValuesAll =“”
不做您认为正在做的事情。我怀疑您认为它正在宣布一个空数组。但是实际上,它创建了一个包含一个空字符串的单个元素的数组。现在,您声明一个新数组,
@CarrierValues
,但是您给它一个值,该值从join()
返回。join()
的全部要点是获取一个值列表(您的数据@Row
),然后将其加入单个字符串。然后,您将单个字符串推到@CarrierValuesall
的末尾。从数组中存储
join()
的结果是没有意义的。join()
的结果仅是一个标量值。因此,在此循环完成运行后,您有一个数组,
@CarrierValuesall
,其中包含数据库表中每个行的这些单字符串之一。这里没有“子阵列”。只是一系列弦。然后,您通过打印
@CarrierValuesall
的内容来确认。这正是我所期望的。您可以从数据库中获取您从逗号分开的数据。
如果您不想要一个字符串,则不要使用
join()
制作一个字符串。取而代之的是,取一份@row
的副本,然后在您的数组中存储一个参考:我不确定您想处理什么数组,但是您可以将其打印出来:
请注意,我已经使用了
@{$ crarierValuesall [$ i]}
用于放置存储在$ carriervaluesall [$ i]
的数组参考,然后将其变成一个数组然后我可以打印。还要注意,我已经将
$ i
的声明移至循环的中,这是更有意义的。
但是,大多数Perl程序员不会将此“ C风格”用于循环,因为它看起来很恐怖并且很难理解。我将此代码写为:
我使用特殊变量
$#array
,而不是计算数组的长度,然后记住停止一个迭代,这给了我阵列中的最后一个索引。You only have one array here - well you have a second array variable, but you only ever store a single, scalar value in it.
Let's look at your code:
Declaring
$i
so far away from where it is used is a bit of a code smell. It'll work fine, of course, but Perl gives you the flexibility to declare variables where you use them - so use it.And
@carrierValuesAll=""
isn't doing what you think it's doing. I suspect you think it's declaring an empty array. But actually it creates an array containing a single element which is an empty string.You now declare a new array,
@carrierValues
, but you give it a value which is returned fromjoin()
. The whole point ofjoin()
is to take a list of values (your data in@row
) and join it together into a single string. You then push that single string onto the end of@carrierValuesAll
.There is no point in storing the result from
join()
in an array. The result fromjoin()
is only ever a single, scalar value.So after this loop has finished running, you have an array,
@carrierValuesAll
which contains one of these single strings for each of the rows in your database table. There are no "subarrays" here. Just an array of strings.You then confirm that by printing the contents of
@carrierValuesAll
.And that prints exactly what I would expect. You get the data that you read from the database separated by commas.
If you don't want a single string, then don't use
join()
to make one. Instead, take a copy of@row
and store a reference to that in your array:I'm not sure what you want to do with your array of arrays, but here's how you could print it out:
Note that I've used
@{$carrierValuesAll[$i]}
to dereference the array reference stored at$carrierValuesAll[$i]
and turn it back into an array which I can then print.Note also that I've moved the declaration of
$i
into thefor
loop where it makes more sense.But most Perl programmers wouldn't use this "C-style" for loop as it looks horrible and is harder to understand. I would write this code as:
Instead of calculating the length of the array and then remembering to stop one iteration before reaching that, I've used the special variable
$#ARRAY
which gives me the index of the last index in the array.