[英]Perl: Array of Array References
Might be repeated somewhere but i've looked at every perl array referencing / dereferencing example i've been able to find and tried running 10+ different combos of this string to get my desired output. 可能会在某处重复,但是我已经查看了每个perl数组引用/解引用示例,我已经找到并尝试运行此字符串的10个以上不同组合以获得所需的输出。 Such as @$array[i], @{$array[i]}, @{@$array[i]} etc.
例如@ $ array [i],@ {$ array [i]},@ {@ $ array [i]}等。
So I have an array of array references. 所以我有一个数组引用数组。 I built this by creating a global array, and then I push smaller temp arrays into that global array.
我通过创建一个全局数组来构建它,然后将较小的临时数组推入该全局数组。 The final outcome looks something like this when Dumper() is ran.
运行Dumper()时,最终结果看起来像这样。
$VAR1 = [ 1 ]; $VAR2 = []; $VAR3 = []; $VAR4 = []; $VAR5 = []; $VAR6 = []; $VAR7 = [ 1 ]; $VAR8 = []; $VAR9 = []; $VAR10 = []; $VAR11 = [ 1 ]; $VAR12 = []; $VAR13 = [ 1 ]; $VAR14 = []; $VAR15 = [ 1 ];
Now I am trying to dereference one of these arrays by using 现在,我尝试通过使用以下方式取消引用这些数组之一
my @arrayRef = @{$redArray[$i]};
print "Here: " . @arrayRef[0];
@redArray is my global array. @redArray是我的全局数组。
The @{$redArray[$i]} part I see as get the reference from the global array, and then deference it which should return as array like [] or [1] or [1,1 .....] 我看到@ {$ redArray [$ i]}部分是从全局数组获取引用,然后对其进行引用,该引用应以[]或[1]或[1,1 .....]的形式返回。
Then on line two I want to get the first element of the array, which would return nothing/error or 1. 然后在第二行,我想获取数组的第一个元素,该元素将不返回任何内容/错误或返回1。
When print "Here: " . 当打印“此处:”时。 @arrayRef[0];
@arrayRef [0]; runs though, the output I see is
虽然运行,我看到的输出是
Here: Here: Here: Here: Here: Here: Here: Here: Here: Here: Here: Here: Here: Here: Here:
As others have mentioned, you're getting confused as to when you should use the $ and @ symbols. 正如其他人所提到的,您何时应该使用$和@符号使您感到困惑。 (To be fair, it's not always obvious in Perl.) The Perl reference tutorial link glenn gave is the best resource.
(公平地说,在Perl中并不总是很明显。)glenn提供的Perl参考教程链接是最好的资源。 Maybe some examples will help:
也许一些例子会有所帮助:
use strict;
use warnings;
my @important_years = (1066, 1492, 1776);
my @recent_years = (2010, 2012, 2014);
my $important_years_ref = \@important_years;
my $recent_years_ref = \@recent_years;
my @redArray = ($important_years_ref, $recent_years_ref);
printf("%s\n", $redArray[0][0]); # 1066
printf("%s\n", $redArray[0][1]); # 1492
printf("%s\n", $redArray[0][2]); # 1776
printf("%s\n", $redArray[1][0]); # 2010
printf("%s\n", $redArray[1][1]); # 2012
printf("%s\n", $redArray[1][2]); # 2014
# Get a reference to the "important years" array
my $important_years_copy_ref = $redArray[0];
printf("%s\n", $important_years_copy_ref->[2]); # 1776
# Or make brand new array by dereferencing
# the "important years" reference stored in @redArray
my @important_years_copy = @{$redArray[0]};
printf("%s\n", $important_years_copy[2]); # 1776
Thanks to glenn jackman and ThisSuitIsBlackNot I was able to figure it out. 多亏了glenn jackman和ThisSuitIsBlackNot,我才得以弄清楚。
The issue is that the reference I was doing above was more or less correct but there was an error later in my code, the reference I went with was, 问题是我在上面所做的引用或多或少是正确的,但后来我的代码中出现错误,我使用的引用是,
my $arrayRef = @redArray[$i];
my @redVal = @{$arrayRef};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.