Perl 如何从没有非空行分隔符的输入创建数组哈希
如何从以下代码和数据创建数组哈希: 这是我的代码:Perl 如何从没有非空行分隔符的输入创建数组哈希,perl,data-structures,Perl,Data Structures,如何从以下代码和数据创建数组哈希: 这是我的代码: 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{$
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
当前执行失败:您在想要的输出数组括号中为哈希写入了
[
,]
试试这个:
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
你可以这样做
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
您可以将Perl的记录分隔符设置为“track name=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
希望这有帮助 始终严格使用;使用警告代码>我写了:)只是不要写在下面。但这一点很好。我会加上:P
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
$ 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 '
]
};
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'
]
};