为什么不是在Perl印刷二维阵列正确? [英] Why isn't the two dimensional array in Perl printing correctly?

查看:122
本文介绍了为什么不是在Perl印刷二维阵列正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理在Perl二维数组是让我头疼。不管怎样,下面是我的问题:

我有一个循环其推动阵列,说@twoOneArray,到另一个数组,说@twoDimArray,然后复位循环的下一次迭代开始之前,然后再次推入@twoDimArray与新组值。当我使用打印此@twoDimArray之一:

 打印翻斗车\\ @twoDimArray;

它给输出

输出

  $ VAR1 = [      [        BB,        AA,        AA
       ]
       $ VAR1-> [0],
       $ VAR1-> [0],
       $ VAR1-> [0]
     ];

或使用循环

 为(我的$ I = 0; $ I< 4; $ I ++){
    为(我的$ J = 0; $ J&4;; $ J ++){
         打印$ twoDimArray [$ i] [$ J] \\ n;
    }
}

中的数据被复制。

输出

行= 0 BB AA AA

行= 1 BB AA AA

行= 2 BB AA AA

行= 3 BB AA AA

等等....

我无法弄清楚,为什么两者的输出方式会错了。如果我打印@twoDimArray每次@twoOneArray插入(移动到循环的下一个迭代,用推送功能后,即之前),那么价值观似乎要被罚款,而不是重复自己,但在打印在一个单一的去似乎给上述错误。类似的问题已经被问<一个href=\"http://stackoverflow.com/questions/1112988/how-can-i-print-a-two-dimensional-array-in-perl\">here但我不知道这是否对我来说很有意义。有什么建议?

code建立二维数组:

 为($ K = 1; $ K&LT; = $计数器; $ķ++){
        @twoOneArray =();当循环再次启动#reset它
        为($ J = 0; $ J&LT; = $ colsInArray; $ J ++){
        #do东西创造@twoOneDim
        }
        推@twoDimArray,\\ @twoOneArray;
        #如果我打印@twoDimArray如果打印细末,用精确值不变
}打印自卸车\\ @twoDimArray; #如果我在这里打出来它搅乱了
打印\\ n;


解决方案

数据::自卸车的输出告诉我,你的问题是不打印的数组。数据::自卸车不会永远说谎(或很少)。

请向我们展示code你用来构建阵列。我敢肯定,误差在code的地方。

更新:

现在您添加的code,它建立了阵,我看你已经堕落成邪陷:您添加的引用@twoOneArray你的外阵列。但是,引用将始终是相同的,通过你的每一次循环中。更改code是这样的:

 为($ K = 1; $ K&LT; = $计数器; $ķ++){
        我@twoOneArray; #真当循环再次启动重置
        为($ J = 0; $ J&LT; = $ colsInArray; $ J ++){
            #do东西创造@twoOneDim
        }
        推@twoDimArray,\\ @twoOneArray;
}

..它应该工作。

Handling two dimensional arrays in Perl is giving me a headache. Anyway, the following is my question:

I have a loop which pushes an array, say @twoOneArray, into another array, say @twoDimArray, and then is reset before the next iteration of loop begins and then again is pushed into @twoDimArray with new set of values. When I print this @twoDimArray using either:

print Dumper \@twoDimArray;

it gives output

OUTPUT

$VAR1 = [

      [

        'BB',

        'AA',

        'AA'
       ],
       $VAR1->[0],
       $VAR1->[0],
       $VAR1->[0]
     ];

or using loops

for (my $i=0; $i<4; $i++){
    for (my $j=0; $j<4; $j++){
         print "$twoDimArray[$i][$j] \n";
    }
}

the data gets duplicated.

OUTPUT

Row=0 BB AA AA

Row=1 BB AA AA

Row=2 BB AA AA

Row=3 BB AA AA

and so on....

I cant figure out why the both the output ways are going wrong. If I print @twoDimArray everytime (before moving to next iteration of loop, i.e. after using push function) @twoOneArray is inserted, then the values seem to be fine and not repeat themselves, but printing it in a single go seems to give the above error. Similar question has been asked here but I am not sure if it makes sense to me. Any suggestions?

Code to build 2D array:

for ($k = 1; $k <= $counter; $k++){
        @twoOneArray = (); #reset it when loop starts again
        for ($j = 0; $j <= $colsInArray; $j++){
        #do stuff to create @twoOneDim
        }
        push @twoDimArray, \@twoOneArray;
        #if I print @twoDimArray if prints fine, with the exact values intact
}

print Dumper \@twoDimArray; #if I print it here it messes up
print "\n";

解决方案

The output of Data::Dumper tells me that your problem is not printing the array. Data::Dumper doesn't ever lie (or only rarely).

Please show us the code you used to build the array. I'm sure that error is in that code somewhere.

UPDATE:

Now that you added the code that builds up the array, I see that you've fallen into an evil trap: You are adding the reference to @twoOneArray to your "outer" array. But the reference will always be the same each time through your loop. Change your code like this:

for ($k = 1; $k <= $counter; $k++){
        my @twoOneArray; # REALLY reset it when loop starts again
        for ($j = 0; $j <= $colsInArray; $j++){
            #do stuff to create @twoOneDim
        }
        push @twoDimArray, \@twoOneArray;
}

.. and it should work.

这篇关于为什么不是在Perl印刷二维阵列正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆