如何创建在Perl多维数组? [英] How can I create multidimensional arrays in Perl?
本文介绍了如何创建在Perl多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点新的Perl,但这里是我想要做的:
I am a bit new to Perl, but here is what I want to do:
my @array2d;
while(<FILE>){
push(@array2d[$i], $_);
}
它不会因为 @ array2d [$ i]
不是一个数组而是一个标量值。
It doesn't compile since @array2d[$i]
is not an array but a scalar value.
如何申报@ array2d作为数组的数组?
How should I declare @array2d as an array of array?
当然,我没有的我有多少行的想法。
Of course, I have no idea of how many rows I have.
推荐答案
为使数组的数组,或者更准确arrayrefs的数组,尝试这样的事情:
To make an array of arrays, or more accurately an array of arrayrefs, try something like this:
my @array = ();
foreach my $i ( 0 .. 10 ) {
foreach my $j ( 0 .. 10 ) {
push @{ $array[$i] }, $j;
}
}
这推到数值为您取消引用数组引用。您应该能够访问一个条目是这样的:
It pushes the value onto a dereferenced arrayref for you. You should be able to access an entry like this:
print $array[3][2];
这篇关于如何创建在Perl多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文