Perl:打印哈希值导致错误:在数组解引用中使用%hashtable中的未初始化值
然而,当我编译时,我得到了一个错误Perl:打印哈希值导致错误:在数组解引用中使用%hashtable中的未初始化值,perl,hash,hashtable,Perl,Hash,Hashtable,然而,当我编译时,我得到了一个错误 my %hashTable; #created my hash while () { #i read through a file and store each word in the file to the hash like so push @{ $hashTable {$key}}, $word; } #end of while #store values of anonymous array at key $key in
my %hashTable; #created my hash
while ()
{
#i read through a file and store each word in the file to the hash like so
push @{ $hashTable {$key}}, $word;
} #end of while
#store values of anonymous array at key $key in array values
@values= @{ $hashTable {$key} };
我希望能够从匿名数组中的哈希中检索值。这是因为
$hashTable{$key}
未定义,而不是数组引用。这可能是因为没有包含key$key
的元素
use of uninitialized value within %hashtable in array dereference at....
发布代码和输入数据。可能是因为while循环没有运行过一次。(你不讨厌调试不完整的代码吗?那是一个永恒的循环…@TheSidhekin抱歉,这不是一个永恒的循环。我只是不想让你们对一些细节感到厌烦,但实际上是在($line=)打开一个文件{……do some my$word=$line他在解释为什么他声称你发布的代码不是你运行的代码:你发布的代码有一个无限循环,阻止打印语句被访问。无论如何,他的观点是,你发布的代码没有显示你所发现的问题,这对包括你自己在内的所有人都是不利的抄写员,如果你有,我们会帮你更好的。
$ perl -wE'my %h; my $k="foo"; $h{$k}=[] if $ARGV[0]; my @a=@{$h{$k}};' 0
Use of uninitialized value within %h in array dereference at -e line 1.
$ perl -wE'my %h; my $k="foo"; $h{$k}=[] if $ARGV[0]; my @a=@{$h{$k}};' 1
$