【发布时间】:2017-02-18 04:33:27
【问题描述】:
我正在解析一个文本文件并将单词作为键存储在哈希中。每个键都有一个数组作为其值,并将该单词在文本中出现的次数作为第一个值存储,并将该单词出现的概率作为第二个值存储在数组中。
例子:
my %ngram = (
"word"=>("how many this word appear in text"," probability of this word")
);
如何访问或检索散列中的数组值?我正在发布我用来完成此任务的代码;我试图打印出这些值,但它会打印为零,如下所示:
0 *** 0
0 *** 0
知道如何检索或访问这些值吗?
while ( scalar @words > $inputs[0])
{
$numerator="@words[0 .. $inputs[0]]";
$denominator= "@words[0 .. ($inputs[0]-1)]";
$nGram{$numerator}[0]=$nGram{$numerator}->[0]++;
$nGram{$denominator}++;
my $freq=$nGram{$numerator}->[0]/$nGram{$denominator};
$nGram{$numerator}[1]=$freq;
print "$nGram{$numerator}->[0] *** $nGram{$numerator}->[1]";
shift @words;
}
【问题讨论】:
标签: perl