当我不重置它时,为什么我的值会改变?
Why does my value change when I am not resetting it?
我有以下示例展示了我正在努力解决的问题。
在玩具示例中,我有一个包含两个级别的数组 @actors
。
我还有一个散列数组 @people
,我将其用于 'look up' @actors
.
中人们的属性
程序的输出应该是:
blue, blue cat, cat
red, red dog, dog
blue, blue cat, cat
red, red dog, dog
但我得到的是:
blue, cat cat, cat
red, dog dog, dog
blue, cat cat, cat
red, dog dog, dog
也就是好像在设置$favanim[$i][$j]
的时候我好像也在覆盖$favcols[$i][$j]
的值。
我怀疑出于某种原因,@actors
是一个二维数组这一事实意味着通过 =
进行的赋值是作为引用而不是作为值,尽管我不知道为什么或如何阻止它。
请帮忙!
玩具程序在这里:(我很抱歉,如果它可以简化,但仍然存在问题 - 我花了一个下午的大部分时间将它简化为这个)
#!/usr/bin/perl -w
my @people = ();
$people[0]{'alternative full names for regexp'} = 'matthew smith|matt smith';
$people[1]{'alternative full names for regexp'} = 'david tennant|dave tennant';
$people[0]{'fav colour'} = 'red';
$people[1]{'fav colour'} = 'blue';
$people[0]{'fav animal'} = 'dog';
$people[1]{'fav animal'} = 'cat';
my @actors = ();
$actors[0][0] = 'David Tennant';
$actors[0][1] = 'Matt Smith';
$actors[1][0] = 'David Tennant';
$actors[1][1] = 'Matt Smith';
my @favcols = @actors;
my @favanim = @actors;
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
my @matching_people = grep{$actors[$i][$j] =~ m/^$_->{'alternative full names for regexp'}$/i} @people;
$favcols[$i][$j] = $matching_people[0]{'fav colour'};
$favanim[$i][$j] = $matching_people[0]{'fav animal'};
print "$matching_people[0]{'fav colour'}, $favcols[$i][$j] $matching_people[0]{'fav animal'}, $favanim[$i][$j]\n";
}
}
尝试使用
@favcols = map { [@$_] } @actors;
@favanim = map { [@$_] } @actors;
深拷贝与浅拷贝。
问题是您正在通过复制 @people
的内容来初始化 @favcols
和 @favanim
,其中包含两个数组引用
这会将 $favcol[0]
和 $favanim[0]
设置为对 相同 数组 [ 'David Tennant', 'Matt Smith' ]
的引用,因此当您修改 $favcols[$i][$j]
然后 $favanim[$i][$j]
您正在覆盖 相同的数组元素
我看不出有任何理由初始化你的数组,如果你将它们声明为
my (@favcols, @favanim);
然后你会发现你的程序如你所愿
顺便说一句,您必须始终 use strict
,并且在命令行
use warnings
优于-w
]
我有以下示例展示了我正在努力解决的问题。
在玩具示例中,我有一个包含两个级别的数组 @actors
。
我还有一个散列数组 @people
,我将其用于 'look up' @actors
.
程序的输出应该是:
blue, blue cat, cat
red, red dog, dog
blue, blue cat, cat
red, red dog, dog
但我得到的是:
blue, cat cat, cat
red, dog dog, dog
blue, cat cat, cat
red, dog dog, dog
也就是好像在设置$favanim[$i][$j]
的时候我好像也在覆盖$favcols[$i][$j]
的值。
我怀疑出于某种原因,@actors
是一个二维数组这一事实意味着通过 =
进行的赋值是作为引用而不是作为值,尽管我不知道为什么或如何阻止它。
请帮忙!
玩具程序在这里:(我很抱歉,如果它可以简化,但仍然存在问题 - 我花了一个下午的大部分时间将它简化为这个)
#!/usr/bin/perl -w
my @people = ();
$people[0]{'alternative full names for regexp'} = 'matthew smith|matt smith';
$people[1]{'alternative full names for regexp'} = 'david tennant|dave tennant';
$people[0]{'fav colour'} = 'red';
$people[1]{'fav colour'} = 'blue';
$people[0]{'fav animal'} = 'dog';
$people[1]{'fav animal'} = 'cat';
my @actors = ();
$actors[0][0] = 'David Tennant';
$actors[0][1] = 'Matt Smith';
$actors[1][0] = 'David Tennant';
$actors[1][1] = 'Matt Smith';
my @favcols = @actors;
my @favanim = @actors;
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
my @matching_people = grep{$actors[$i][$j] =~ m/^$_->{'alternative full names for regexp'}$/i} @people;
$favcols[$i][$j] = $matching_people[0]{'fav colour'};
$favanim[$i][$j] = $matching_people[0]{'fav animal'};
print "$matching_people[0]{'fav colour'}, $favcols[$i][$j] $matching_people[0]{'fav animal'}, $favanim[$i][$j]\n";
}
}
尝试使用
@favcols = map { [@$_] } @actors;
@favanim = map { [@$_] } @actors;
深拷贝与浅拷贝。
问题是您正在通过复制 @people
的内容来初始化 @favcols
和 @favanim
,其中包含两个数组引用
这会将 $favcol[0]
和 $favanim[0]
设置为对 相同 数组 [ 'David Tennant', 'Matt Smith' ]
的引用,因此当您修改 $favcols[$i][$j]
然后 $favanim[$i][$j]
您正在覆盖 相同的数组元素
我看不出有任何理由初始化你的数组,如果你将它们声明为
my (@favcols, @favanim);
然后你会发现你的程序如你所愿
顺便说一句,您必须始终 use strict
,并且在命令行
use warnings
优于-w
]