Hash不会在Perl中打印

时间:2014-01-19 12:08:03

标签: perl hash

我有一个哈希:

while( my( $key, $value ) = each %sorted_features ){
  print "$key: $value\n";
}

但我无法获得$value的正确值。它给了我:

intron: ARRAY(0x3430440)
source: ARRAY(0x34303b0)
exon: ARRAY(0x34303f8)
sig_peptide: ARRAY(0x33f0a48)
mat_peptide: ARRAY(0x3430008)

为什么?

3 个答案:

答案 0 :(得分:10)

您的值是数组引用。你需要做一些像

这样的事情
while( my( $key, $value ) = each %sorted_features ) {
  print "$key: @$value\n";
}

换句话说,取消引用参考。如果您不确定数据是什么样的,最好使用Data::Dumper模块:

use Data::Dumper;
print Dumper \%sorted_features;

你会看到类似的东西:

$VAR1 = {
          'intron' => [
                        1,
                        2,
                        3
                      ]
        };

其中{表示哈希引用的开头,[表示数组引用。

答案 1 :(得分:0)

您的哈希值是数组引用。您需要编写其他代码来显示这些数组的内容,但如果您只是调试那么使用Data::Dumper这样可能更简单

use Data::Dumper;
$Data::Dumper::Useqq = 1;

print Dumper \%sorted_features;

顺便说一下,哈希的名字%sorted_features让我担心。哈希本质上是未排序的,each检索元素的顺序基本上是随机的。

答案 2 :(得分:0)

你也可以使用Data::Dumper::Pertidy通过Perltidy运行Data :: Dump的输出。

#!/usr/bin/perl -w

use strict;
use Data::Dumper::Perltidy;

my $data = [{title=>'This is a test header'},{data_range=>
           [0,0,3,    9]},{format     => 'bold' }];

print Dumper $data;

<强>打印

$VAR1 = [
    { 'title'      => 'This is a test header' },
    { 'data_range' => [ 0, 0, 3, 9 ] },
    { 'format'     => 'bold' }
];