【发布时间】:2014-11-22 17:30:00
【问题描述】:
我有一个创建 Document 对象的包:
package Document;
sub new
{
my ($class, $id) = @_;
my $self = {};
bless $self, $class;
$self = {
_id => $id,
_title => (),
_words => ()
};
bless $self, $class;
return $self;
}
sub pushWord{
my ($self, $word) = @_;
if(exists $self->{_words}{$word}){
$self->{_words}{$word}++;
}else{
$self->{_words}{$word} = 0;
}
}
我称之为:
my @docs;
while(counter here){
my $doc = Document->new();
$doc->pushWord("qwe");
$doc->pushWord("asd");
push(@docs, $doc);
}
在第一次迭代中,第一个$doc 的哈希有两个元素。在第二次迭代中,第二个$doc 的哈希有四个元素(包括第一个元素中的两个)。但是当我使用那个实体对象(创建一个Document 的数组)时,我得到:
- 散列大小为 x 的 Document-1
- 散列大小为 x+y 的 Document-2
- 散列大小为 x+y+z 的 Document-3
为什么哈希的大小会增加? Document-3 包含 Document-1 和 Document-2 中的所有哈希内容。这与祝福或取消定义变量有关吗?构造函数错了吗?
谢谢:D
【问题讨论】:
-
“哈希大小”是什么意思?
-
如果不清楚,抱歉。我的意思是哈希的总元素。所以 Document-2 的 hash 拥有 Document-1 的 hash 的所有元素。
-
这不是我得到的行为。您的构造函数肯定是错误的,但请同时显示您的调用代码。
-
编辑了我如何称呼它的简化。这个想法是我想计算每个文档中每个单词的频率。
-
我无法复制问题。