Perl:在 Foreach 循环中分配对数组元素的引用
我真的很想自己解决这个问题,但我的脸现在因为不断撞到这堵砖墙而受伤。
我正在尝试加载 9 个文本文件,每个文件由 7 行、每行 7 个字符组成的矩阵(用空格分隔),然后将每个引用的矩阵保存到数组中的一个元素。我在每个文件中读取得很好,但是当我访问我的数组时,所有元素都是相同的。我一直在寻找解决方案,但我的问题在任何地方都没有得到解答,或者(更有可能)我不理解答案。这是我的代码的问题部分:
my @boardarray = (1, 2, 3, 4, 5, 6, 7, 8, 9);
sub LoadBoards {
my (@board, $infile, @allboards);
my $i = 1;
@allboards = @boardarray;
foreach (@allboards) {
my $infile = "board" . $i . "\.brd";
open FILE, "< $infile" or die $!;
my $line = 0;
while (<FILE>) {
chomp $_;
my @chars = split (/ /,$_);
$board[$line] = [@chars];
$line++;
}
my $tempboard = \@board;
DisplayOneBoard($tempboard); print ("\n"); #Test A
$boardarray[$i-1] = \@board; #Problem line?
DisplayOneBoard($boardarray[$i-1]); print ("\n"); #Test B
DisplayOneBoard($boardarray[0]); print ("\n----\n"); #Test C
$i++;
}
}
-我尝试将变量分配为 @boardarray 的元素,没有任何更改。
-我在 foreach 循环中使用@boardarray,并将其更改为复制的@allboards,没有任何改进。
我希望“测试 A”和“测试 B”行相同,并且“测试 C”行保留我加载的第一个矩阵。但是,每次迭代这三个行都是相同的。
(对于迭代 1,它们都是矩阵 1。对于迭代 2,它们都是矩阵 2,等等)
最后,所有元素都是完全相同的矩阵(矩阵 9)。
任何帮助将不胜感激。谢谢。
I really wanted to figure this out myself, but my face is now hurting from continually running into this brick wall.
I'm trying to load 9 text files, each consisting of a matrix of 7 rows of 7 characters seperated by spaces and then save each referenced matrix to an element in an array. I am reading in each file just fine, but when I go to access my array all of the elements are the same. I've been searching for a solution and either my question isn't answered anywhere, or (more likely) I'm not understanding the answer. Here's the problem section of my code:
my @boardarray = (1, 2, 3, 4, 5, 6, 7, 8, 9);
sub LoadBoards {
my (@board, $infile, @allboards);
my $i = 1;
@allboards = @boardarray;
foreach (@allboards) {
my $infile = "board" . $i . "\.brd";
open FILE, "< $infile" or die $!;
my $line = 0;
while (<FILE>) {
chomp $_;
my @chars = split (/ /,$_);
$board[$line] = [@chars];
$line++;
}
my $tempboard = \@board;
DisplayOneBoard($tempboard); print ("\n"); #Test A
$boardarray[$i-1] = \@board; #Problem line?
DisplayOneBoard($boardarray[$i-1]); print ("\n"); #Test B
DisplayOneBoard($boardarray[0]); print ("\n----\n"); #Test C
$i++;
}
}
-I've tried assinging variables as the elements of @boardarray with no change.
-I was using @boardarray in the foreach loop and changed it to the copied @allboards with no improvement.
I expect the 'Test A' and 'Test B' lines to be the same and for the 'Test C' line to stay the first matrix I loaded in. However, all three are the same for every iteration.
(For iteration 1 they are all matrix 1. For iteration 2 they are all matrix 2, etc.)
At the end all the elements are the exact same matrix (matrix 9).
Any assistance would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您每次循环时都重复使用相同的
@board
。当您将该板的引用推送到@boardarray
上时,您每次都会推送指向同一@board
的引用。修复方法很简单,只需将my @board
移动到foreach
循环内部即可;每次都会创建一个新的@board
。The problem is that you are re-using the same
@board
each time through your loop. When you push a reference to that board onto@boardarray
, you are pushing a reference pointing the same@board
each time. The fix is simple, just movemy @board
to the inside of yourforeach
loop; this creates a new@board
each time through.通过进一步分解代码并使用数组作为带有推送/弹出的堆栈,您可能会拥有更好的运气:
You might have better luck by breaking down the code a bit more and using arrays as stacks with push/pop: