提供未定义值的Perl哈希

提供未定义值的Perl哈希,perl,perl-hash,Perl,Perl Hash,If试图使用perl散列编写一段代码。数据中没有未定义的值或新行(也尝试从文件中提供相同的输入)。但是,当使用数据转储程序或传统方式打印时,我得到一个'作为键,unde作为其值。为什么会这样?我错过了什么明显的东西吗 节目: use strict; use Data::Dumper; my %i_hash = undef; my %p_hash = undef; while (<DATA>) { chomp; my @line = split(/\h+/);

If试图使用
perl散列
编写一段代码。
数据中没有未定义的值或新行(也尝试从文件中提供相同的输入)。但是,当使用数据转储程序或传统方式打印时,我得到一个
'
作为键,
unde
作为其值。为什么会这样?我错过了什么明显的东西吗

节目:

use strict;
use Data::Dumper;

my %i_hash = undef;
my %p_hash = undef;

while (<DATA>) {
    chomp;
    my @line = split(/\h+/);
    $i_hash{ $line[0] } = $line[1];    # Interactions Hash
    $p_hash{ $line[0] } = $line[2];    # PMIDs Hash

}
print Dumper( \%i_hash, \%p_hash );

__DATA__
AAA     BBB     PMID_1
BBB     AAA     PMID_2
CCC     AAA     PMID_3
DDD     CCC     PMID_4
EEE     FFF     PMID_1
FFF     GGG     PMID_6

始终使用
使用警告

my %i_hash = undef;
my %p_hash = undef;
给予

您没有为value元素提供值,因此在发出警告后使用
unde

键是字符串,
unde
的字符串化是空字符串,但这样做会发出警告

你想要:

my %i_hash;
my %p_hash;

谢谢,它起作用了。我通常使用这种语法初始化数组和标量。我用同样的方法做杂烩。顺便说一句,我什么时候可以使用这种语法。数组也是个坏主意<代码>my@array=undef
my@array相同$数组[0]=未定义。对于标量来说,这只是无用的噪声。这完全是错误的。@aravindramesh,请注意,当您键入
my@array=unde;if(@array){print'true';}
此处的'if'语句将为true,因为您的数组已经有1个元素(unde)。您会感到惊讶,但您不应该也从subs返回unde,因为
my@ar=f();if(@ar){print'true';}sub f{return undef;}
“if”出于同样的原因将在此处为true
Odd number of elements in hash assignment at a.pl line 6.
Use of uninitialized value in list assignment at a.pl line 6.
Odd number of elements in hash assignment at a.pl line 7.
Use of uninitialized value in list assignment at a.pl line 7.
my %i_hash;
my %p_hash;