2014-01-19 81 views
1

打印我的哈希:哈希並不在Perl

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) 

爲什麼呢?

回答

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 
         ] 
     }; 

{表示哈希引用的開始,和[數組引用。

0

你的散列值是數組引用。您需要編寫額外的代碼,以顯示這些數組的內容,但如果你是剛剛調試那麼它可能是更容易使用Data::Dumper這樣

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

print Dumper \%sorted_features; 

而且,順便說一句,你的哈希的後顧之憂名稱%sorted_features我。哈希本質上是未排序的,並且each檢索元素的順序基本上是隨機的。

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' } 
];