代码之家  ›  专栏  ›  技术社区  ›  Aaron

如何在Perl中打印二维数组?

  •  4
  • Aaron  · 技术社区  · 16 年前

    push (@matrix, \@a1Comparea2);
    push (@matrix, \@a3Comparea4);
    

    $VAR1 = [
              [
                '1 6',
                '2 7',
                '3 8',
                '4 9',
                '5 10'
              ],
              $VAR1->[0],
              $VAR1->[0],
              $VAR1->[0],
              $VAR1->[0],
              [
                '7 12',
                '8 13',
                '9 14',
                '10 15',
                '11 16'
              ],
              $VAR1->[5],
              $VAR1->[5],
              $VAR1->[5],
              $VAR1->[5]
            ];
    

    for (my $j= 0; $j < $rows; $j++)
    {
        for (my $k= 0; $k < @a1; $k++)
        {
            print "Row:$j Col:$k = $matrix[$j][$k]\n";
        }
    }
    

    Row:0 Col:0 = 1 6
    Row:0 Col:1 = 2 7
    Row:0 Col:2 = 3 8
    Row:0 Col:3 = 4 9
    Row:0 Col:4 = 5 10
    Row:1 Col:0 = 1 6
    Row:1 Col:1 = 2 7
    Row:1 Col:2 = 3 8
    Row:1 Col:3 = 4 9
    Row:1 Col:4 = 5 10
    

    如您所见,数据是重复的。

    2 回复  |  直到 5 年前
        1
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前


    你应该使用 @ $
    article for a quick reference .


    这篇文章给出了一个简单的例子。

    @matrix = (
            [3, 4, 10],
            [2, 7, 12],
            [0, 3, 4],
            [6, 5, 9],
          );
    

    for($row = 0; $row < 4; $row++) {
        for($col = 0; $col < 3; $col++) {
            print "$matrix[$row][$col] ";
        }
        print "\n";
    }
    
        2
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    for (my $j=0; $j < $rows; $j++)
    {
        for (my $k=0; $k < @a1; $k++)
        {
            print "Row:$j Col:$k = $matrix[$not_j][$k]\n";
        }
    }
    

    $not_j 将始终计算为0,从而产生您的输出。