【发布时间】:2013-12-26 04:24:17
【问题描述】:
如果尝试使用perl hash 编写一段代码。 __DATA__ 中没有未定义的值或新行(也尝试从文件中提供相同的输入)。但是在使用数据转储器或传统方式打印时,我得到一个 '' 作为键和 undef 作为它的值。为什么会这样?我错过了什么明显的东西吗?
程序:
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
输出:
$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'
};
【问题讨论】: