简体   繁体   English

二维阵列打印作为参考

[英]2D Array Printing as Reference

I have the code similar to below:我有类似于下面的代码:

my @array1 = (); #2d array to be used
my $string1 = "blank1";
my $string2 = "blank2";
my $string3 = "blank3";

my @temp = ($string1, $string2, $string3);
push (@array1, \@temp);

The reason I am assigning the strings and then putting them into an array is because they are in a loop and the values get updated in the loop (@array1 is not declared in the loop).我分配字符串然后将它们放入数组的原因是因为它们处于循环中并且值在循环中得到更新(@array1 未在循环中声明)。

When I run my program, it only gives me a reference to an array rather than an actual 2D array.当我运行我的程序时,它只给了我一个数组的引用,而不是一个实际的二维数组。 How can I get it to print out the content as a 2D array and not as a reference or flattened out to a 1D array?如何让它将内容打印为 2D 数组而不是作为参考或展平为 1D 数组?

I would like an output like [[blank1, blank2, blank3],....] so i can access it like $array1[i][j]我想要一个像[[blank1, blank2, blank3],....]这样的输出,所以我可以像$array1[i][j]一样访问它

An array can only have scalars for elements.数组只能有元素的标量。 Thus this includes references, to arrays for example, what enables us to build complex data structures.因此,这包括对数组的引用,例如,使我们能够构建复杂数据结构的东西。 See perldsc , Tom's Perl Data Structure Cookbook .请参阅perldscTom 的 Perl Data Structure Cookbook

Elements of those ("second-level") arrays are accessed by dereferencing , so $array1[0]->[1] is the second element of the array whose reference is the first element of the top-level array ( @array1 ).这些(“二级”)数组的元素是通过解引用来访问的,所以$array1[0]->[1]是数组的第二个元素,其引用是顶级数组的第一个元素( @array1 ) . Or, for convenience, a simpler syntax is allowed as well: $array1[0][1] .或者,为方便起见,也允许使用更简单的语法: $array1[0][1]

If we want a list of all elements of a second-level array then dereference it with @ , like:如果我们想要一个二级数​​组的所有元素的列表,那么用@取消引用它,比如:

my @l2 = @{ $array1[0] };   # or, using
my @l2 = $array1[0]->@*;    # postfix dereferencing

Or, to get just a few elements of the array, but in one scoop -- a slice或者,只获取数组的几个元素,但在一个勺子中 - 一个切片

my @l2_slice = @{$array1[0]}[1..2];  # or
my @l2_slice = $array1[0]->@[1..2];  # postfix reference slice

what returns the list with the second and third elements of the same second-level array.什么返回具有相同二级数组的第二个和第三个元素的列表。

The second lines are of a newer syntax called postfix dereferencing , stable as of v5.24.第二行是一种称为postfix dereferencing的较新语法,自 v5.24 起稳定。 It avails us with the same logic for getting elements as when we drill for a single one, by working left-to-right all the way.它为我们提供了与我们钻取单个元素时相同的获取元素的逻辑,从左到右一直工作。 So ->@* to get a list of all elements for an arrayref, ->%* for a hashref (etc).所以->@*获取 arrayref 的所有元素的列表, ->%*获取 hashref (等)。 See for instance a perl.com article and an Effective Perler article .参见例如perl.com 文章Effective Perler文章

There is a thing to warn about when it comes to multidimensional structures built with references.当涉及到使用引用构建的多维结构时,需要注意一件事。 There are two distinct ways to create them : by using references to existing, named, variables有两种不同的方法来创建它们:通过使用对现有的、命名的、变量的引用

my @a1 = 5 .. 7;
my %h1 = ( a => 1, b => 2 );

my @tla1 = (\@a1, \%h1);

or by using anonymous ones, where arrayrefs are constructed by [] and hashrefs by {}或者使用匿名的,arrayrefs 由[]构造,hashrefs 由{}

my @tla2 = ( [ 5..7 ], { a => 1, b => 2 } );

The difference is important to keep in mind.记住这一点很重要。 In the first case, the references to variables that the array carries can be used to change them -- if we change elements of @tla1 then we change the referred variables在第一种情况下,可以使用对数组携带的变量的引用来更改它们——如果我们更改@tla1的元素,那么我们会更改引用的变量

$tla1[0][1] = 100;  # now @a1 == 5, 100, 7

Also, changing variables with references in @tla1 is seen via the top-level array as well.此外,通过顶级数组也可以看到在@tla1中使用引用更改变量。

With anonymous array and hash in @tla this isn't the case as @tla accesses an independent set of the data, which cannot be accessed (and changed) in any other way.@tla中使用匿名数组和散列时,情况并非如此,因为@tla访问一组独立的数据,无法以任何其他方式访问(和更改)这些数据。 Both of these have their uses.这两种都有它们的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM