Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Assigning References to Array Elements in a Foreach Loop

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.

like image 592
Royal Connell Avatar asked May 13 '11 18:05

Royal Connell


People also ask

How do you reference an array in Perl?

An Example First, remember that [1, 2, 3] makes an anonymous array containing (1, 2, 3) , and gives you a reference to that array. @a = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); @a is an array with three elements, and each one is a reference to another array.

How do I dereference an array reference in Perl?

Perl array references Notice that the backslash operator ( \ ) is also used in front of array variable like the scalar variable. Third, in the for loop, we dereferenced the reference $ar by using @$ar . You can use curly braces @{$ar} if you want. However, shorter is better.


1 Answers

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.

like image 171
friedo Avatar answered Sep 23 '22 00:09

friedo