1

如果试图使用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'
    };
4

1 回答 1

3

一直用use warnings;

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

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.

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

键是字符串,字符串化undef是空字符串,尽管这样做会发出警告。

你要:

my %i_hash;
my %p_hash;
于 2013-12-26T04:27:34.503 回答