Perl:在 Foreach 循环中分配对数组元素的引用

发布于 2024-11-06 22:38:32 字数 1284 浏览 1 评论 0原文

我真的很想自己解决这个问题,但我的脸现在因为不断撞到这堵砖墙而受伤。

我正在尝试加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-11-13 22:38:32

问题是您每次循环时都重复使用相同的 @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 move my @board to the inside of your foreach loop; this creates a new @board each time through.

我很坚强 2024-11-13 22:38:32

通过进一步分解代码并使用数组作为带有推送/弹出的堆栈,您可能会拥有更好的运气:

sub load_file {
    my ($filename) = @_;
    open my $file, '<', $filename or die $!;
    my @array;
    while (<$file>) {
        chomp $_;
        my @chars = split (/ /,$_);
        push @array, \@chars;    ### adds a reference to the char line 
                                 ### array to the end of the array
    }
    return \@array;              ### return a ref to the 2-d array
}

sub load_files {
    my ($num) = @_;
    my %boards;     ### A hash, so we can refer to loaded arrays
                    ### with a string ID

    for my $filenum ( 1 .. $num ) {
         my $filename = "board" . $filenum . "\.brd";
         $boards{$filenum} = load_file($filename);
    }

    return \%boards;   ### return a ref to the hash of 2-d arrayrefs
}

### use it now...
my $boards = load_files(9); ### load 9 files.

DisplayOneBoard($boards->{6}); ### dereference our hashref, pass board in
                               ### key '6'to be displayed

You might have better luck by breaking down the code a bit more and using arrays as stacks with push/pop:

sub load_file {
    my ($filename) = @_;
    open my $file, '<', $filename or die $!;
    my @array;
    while (<$file>) {
        chomp $_;
        my @chars = split (/ /,$_);
        push @array, \@chars;    ### adds a reference to the char line 
                                 ### array to the end of the array
    }
    return \@array;              ### return a ref to the 2-d array
}

sub load_files {
    my ($num) = @_;
    my %boards;     ### A hash, so we can refer to loaded arrays
                    ### with a string ID

    for my $filenum ( 1 .. $num ) {
         my $filename = "board" . $filenum . "\.brd";
         $boards{$filenum} = load_file($filename);
    }

    return \%boards;   ### return a ref to the hash of 2-d arrayrefs
}

### use it now...
my $boards = load_files(9); ### load 9 files.

DisplayOneBoard($boards->{6}); ### dereference our hashref, pass board in
                               ### key '6'to be displayed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文