[英]Perl hash giving undef values
If was trying to write a piece of code using perl hash
. 如果试图使用
perl hash
编写一段代码。 There are no undefined values or new lines in __DATA__
(tried giving same input from file also). __DATA__
中没有未定义的值或新行(尝试从文件中提供相同的输入)。 But while printing using data dumper or the traditional way I am getting a ''
as key and undef
as its value. 但是在使用数据转储器或传统方式进行打印的过程中,我得到了一个
''
关键和undef
”的价值。 Why is this happening ? 为什么会这样? Am I missing something obvious ?
我错过了一些明显的东西吗
Program: 程序:
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
OutPut: 输出:
$VAR1 = {
'' => undef,
'FFF' => 'GGG',
'CCC' => 'AAA',
'BBB' => 'AAA',
'EEE' => 'FFF',
'DDD' => 'CCC',
'AAA' => 'BBB'
};
$VAR2 = {
'' => undef,
'FFF' => 'PMID_6',
'CCC' => 'PMID_3',
'BBB' => 'PMID_2',
'EEE' => 'PMID_1',
'DDD' => 'PMID_4',
'AAA' => 'PMID_1'
};
Always use use warnings;
始终使用
use warnings;
! !
my %i_hash = undef;
my %p_hash = undef;
gives 给
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.
You didn't provide a value for the value element, so undef
is used after issuing a warning. 您没有为value元素提供值,因此在发出警告后使用
undef
。
Keys are strings and the stringification of undef
is the empty string, though doing so issues a warning. 键是字符串,
undef
的字符串化是空字符串,但这样做会发出警告。
You want: 你要:
my %i_hash;
my %p_hash;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.