简体   繁体   English

在Perl类中定义2D数组

[英]Defining 2D array inside class of Perl

I am new to Perl and I am trying to define a 2D array as an attribute of my class in Perl. 我是Perl的新手,我正在尝试将2D数组定义为我在Perl中的类的属性。 I define my class as follows, 我将课程定义如下

sub new{
my $class = shift;
my $self = {};

my @board = [];
for (my $i = 0; $i < 8; $i++){
    for(my $j = 0; $j < 8; $j++){
        $board[$i][$j] = '.';
    }
}
$self->{board} = @board;
bless($self, $class);
return $self;
}

But later on when I try to access the board field like this 但是后来当我尝试像这样访问板子领域时

$self->{board}[$i][$j] = ' ';

I got an error saying 我说错了

Can't use string ("8") as an ARRAY ref while "strict refs" in use

Can anyone tell me what is the correct way of doing this? 谁能告诉我这样做的正确方法是什么? I do not want to just delete use strict. 我不想只是删除使用严格。

I changed your code to what I'm sure was your intention, see the lines changed and comment # not 我将您的代码更改为我确定的意图,请参见更改的行并注释#

sub new{
my $class = shift;
my $self = {};

my @board = ();  # not []
for (my $i = 0; $i < 8; $i++){
    for(my $j = 0; $j < 8; $j++){
        $board[$i][$j] = '.';
    }
}
$self->{board} = \@board; # not @board
bless($self, $class);
return $self;
}

or 要么

sub new{
my $class = shift;
my $self = {};

my $board = []; # not @board
for (my $i = 0; $i < 8; $i++){
    for(my $j = 0; $j < 8; $j++){
        $board->[$i][$j] = '.';
    }
}
$self->{board} = $board; # not @board
bless($self, $class);
return $self;
}

about your my @board=[]; 关于my @board=[]; is the same as =([],); =([],); assign a list (that perl calls ARRAY) whose first element is a reference to an ARRAY to @board, but this is neither what make your code fail because you overwrite this empty array reference allocation and assignment to position zero. 将一个第一个元素是对ARRAY的引用的列表(perl称为ARRAY)分配给@board,但这都不会使您的代码失败,因为您覆盖了此空数组引用分配和位置0的分配。 The @board is a list not a reference to it as $self->{board} expect @board是一个列表,而不是$ self-> {board}期望的引用

You need to place a reference to array inside your $self hash. 您需要在$self哈希中放置对数组的引用。 Right now you're placing a value of array in scalar context - which is its length 8 . 现在,您要在标量上下文中放置一个数组的值-它的长度8 Of course you can't later use this as a reference to anything. 当然,您以后不能使用它作为对任何事物的引用。

$self->{board} = \@board;

Others have given you explanations of what the problem is with your constructor. 其他人则向您解释了构造函数的问题所在。 I'll add, that you can simplify it a bit by omitting the $board variable and using C-style loops. 我要补充一点,您可以通过省略$board变量并使用C风格的循环来简化它。

sub new {
  my $class = shift;

  my $self = {};
  $self->{board} = [];

  for my $i (1 .. 8) {
    for my $j (1 .. 8) {
      $self->{board}[$i][$j] = '.';
    }
  }

  return bless $self, $class;
}

I'd also add the following three methods which make it easier to set and get the elements of the board: 我还将添加以下三种方法,这些方法可以更轻松地设置和获取木板的元素:

sub board {
  my $self = shift;
  return $self->{board};
}

sub set_board_element {
  my $self = shift;
  my ($i, $j, $val) = @_;

  $self->board->[$i][$j] = $val;
}

sub get_board_element {
  my $self = shift;
  my ($i, $j) = @_;

  return $self->board->[$i][$j];
}

Have you considered using Moose to write your class? 您是否考虑过使用Moose编写课程? It will make your life easier. 这将使您的生活更轻松。 In particular, Array traits seem like a good fit for your problem. 特别地, 数组特征似乎很适合您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM