2014-01-15 16 views
1

如何創建陣列的從下面的代碼和數據的散列:如何在沒有非空行創建從輸入陣列的散列隔板

這是我的代碼:

use strict; 
use warnings; 
use Data::Dumper; 

my %hash; 
while(<DATA>) { 
    chomp; 
    my $line = $_; 
    print "$line\n"; 
    my ($id) = /^(track.*$)/; 
    my ($mem) = /^(chr22.*$)/; 
    print " ID: $id - $mem\n"; 
    push @{$hash{$id}},$mem; 
} 

print Dumper \%hash; 

__DATA__ 
track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 
chr22[Target-Scrambled-Inversion] 29835 30134 
chr22[Target-Scrambled-Inversion] 29154 29453 
track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872 
chr22[Target-Scrambled-Inversion] 29141 29440 

因此,每個元素都具有散列track作爲鍵和隨後的chr22條目作爲其成員。

在這一天結束時,我想創建此輸出:

$VAR = [ "track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263" => 
      ["chr22[Target-Scrambled-Inversion] 29835 30134", 
      "chr22[Target-Scrambled-Inversion] 29154 29453"], 
      "track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872" => 
      ["chr22[Target-Scrambled-Inversion] 29141 29440" ]]; 

目前執行失敗:https://eval.in/89547

回答

0

你在你想要的輸出數組括號[]爲你寫的哈希。

試試這個:

use strict; 
use warnings; 
my %hash; 
my $track_chr = 0; 
while(my $line = <DATA>) { 
    chomp $line; 
    if ($line =~ m/^track/) { 
    $track_chr = $line; 
    #$hash->{$line}; 
    } 
    if ($track_chr && $line =~ m/^chr/) { 
    push @{$hash{$track_chr}},$line; 
    } 
} 

print Dumper \%hash; 

__DATA__ 
track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 
chr22[Target-Scrambled-Inversion] 29835 30134 
chr22[Target-Scrambled-Inversion] 29154 29453 
track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872 
chr22[Target-Scrambled-Inversion] 29141 29440 

很簡單。輸出:

$VAR1 = { 
      'track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 ' => [ 
                       'chr22[Target-Scrambled-Inversion] 29835 30134 ', 
                       'chr22[Target-Scrambled-Inversion] 29154 29453 ' 
                      ], 
      'track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872' => [ 
                      'chr22[Target-Scrambled-Inversion] 29141 29440 ' 
                      ] 
     }; 
+1

*總是*'使用嚴格;使用警告;' – Kenosis

+1

我沒有:)只是不寫在那裏。但好點。我會添加它:P – 2014-01-16 06:37:32

1

你可以做這樣的事情......

use strict; 
use warnings; 
use Data::Dumper; 
$Data::Dumper::Indent = 1; # personal preference for readability 

my %hash; 
my $key; 

# iterate over a line at a time 
while (my $line = <DATA>) { 
    chomp $line; 

    # if the line begins with "track" store the key 
    if ($line =~ /^track/) { 
     $key = $line; 
    } elsif ($line =~ /^chr22/) { 
     # skip this line if we were not able to set a key... 
     next if !defined $key; 
     # else we push onto the array 
     push @{$hash{$key}}, $line; 
    } 
} 

print Dumper \%hash; 

__DATA__ 
track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 
chr22[Target-Scrambled-Inversion] 29835 30134 
chr22[Target-Scrambled-Inversion] 29154 29453 
track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872 
chr22[Target-Scrambled-Inversion] 29141 29440 
some random line 
more randomnessssssss 

OUTPUT:

$ perl test.pl 
$VAR1 = { 
    'track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 ' => [ 
    'chr22[Target-Scrambled-Inversion] 29835 30134 ', 
    'chr22[Target-Scrambled-Inversion] 29154 29453 ' 
    ], 
    'track name=chr22[Target-Scrambled-Inversion]_30604_INV_8872' => [ 
    'chr22[Target-Scrambled-Inversion] 29141 29440 ' 
    ] 
}; 
1

您可以設置Perl的記錄分隔符爲 「曲目名稱= chr22」 讀你這些數據塊中的數據:

use strict; 
use warnings; 
use Data::Dumper; 

my %hash; 
local $/ = 'track name=chr22'; 

while (<DATA>) { 
    chomp; 
    my @items = split /\n/ or next; 
    push @{ $hash{ $/ . $items[0] } }, @items[ 1 .. $#items ]; 
} 

print Dumper \%hash; 

__DATA__ 
track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 
chr22[Target-Scrambled-Inversion] 29835 30134 
chr22[Target-Scrambled-Inversion] 29154 29453 
track name=chr22[Target-Scrambled-Inversion]_30604_INV 
chr22[Target-Scrambled-Inversion] 29141 29440 

輸出:

$VAR1 = { 
      'track name=chr22[Target-Scrambled-Inversion]_29112_INS_-263 ' => [ 
                       'chr22[Target-Scrambled-Inversion] 29835 30134 ', 
                       'chr22[Target-Scrambled-Inversion] 29154 29453 ' 
                      ], 
      'track name=chr22[Target-Scrambled-Inversion]_30604_INV' => [ 
                     'chr22[Target-Scrambled-Inversion] 29141 29440' 
                     ] 
     }; 

希望這會有所幫助!