我的数据如下:
some info
some info
[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cy
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution
[Term]
id: GO:0000002
name: mitochondrial genome maintenance
namespace: biological_process
def: "The maintenance of the structure and integrity of the mitochondrial genome; includes replication and segregation of the mitochondrial chromosome." [GOC:ai, GOC:vw]
is_a: GO:0007005 ! mitochondrion organization
[Typedef]
id: regulates
name: regulates
xref: RO:0002211
transitive_over: part_of ! part_of
请注意,该文件的末尾包含空格。
我想要做的是解析以[Term]
开头的每个块并获取id
,name
和namespace
。在一天结束时,数组的散列如下:
$VAR = ['GO:0000001' => ["mitochondrion inheritance","biological_process"],
'GO:0000002' => ["mitochondrial genome maintenance","biological_process"];
我怎样才能解决这个问题?
我坚持使用这段代码:
#!/usr/bin/perl
use Data::Dumper;
my %bighash;
while(<DATA>) {
chomp;
my $line = $_;
my $term = "";
my $id = "";
my $name ="";
my $namespace ="";
if ($line =~ /^\[Term/) {
$term = $line;
}
elsif ($line =~ /^id: (.*)/) {
$id = $1;
}
elsif ($line =~ /^name: (.*)/) {
$name = $1;
}
elsif ($line =~ /^namespace: (.*)/) {
$namespace = $1;
}
elsif ($line =~ /$/) {
$bighash{$id}{$name} = $namespace;
}
}
print Dumper \%bighash;
__DATA__
some info
some info
[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cy
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution
[Term]
id: GO:0000002
name: mitochondrial genome maintenance
namespace: biological_process
def: "The maintenance of the structure and integrity of the mitochondrial genome; includes replication and segregation of the mitochondrial chromosome." [GOC:ai, GOC:vw]
is_a: GO:0007005 ! mitochondrion organization
[Typedef]
id: regulates
name: regulates
xref: RO:0002211
transitive_over: part_of ! part_of
答案 0 :(得分:5)
如果将Perl的输入记录分隔符设置为''
(local $/ = '';
),则会以段模式读取数据,即以空行分隔的块。接下来,您可以使用正则表达式从该块中捕获所需的部分。例如:
use strict;
use warnings;
use Data::Dumper;
local $/ = '';
my %hash;
while (<DATA>) {
next unless /^\[Term\]/;
my ($id) = /id:\s+(.+)/;
my ($name) = /name:\s+(.+)/;
my ($namespace) = /namespace:\s+(.+)/;
push @{ $hash{$id} }, ( $name, $namespace );
}
print Dumper \%hash;
__DATA__
[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cy
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution
[Term]
id: GO:0000002
name: mitochondrial genome maintenance
namespace: biological_process
def: "The maintenance of the structure and integrity of the mitochondrial genome; includes replication and segregation of the mitochondrial chromosome." [GOC:ai, GOC:vw]
is_a: GO:0007005 ! mitochondrion organization
[Typedef]
id: regulates
name: regulates
xref: RO:0002211
transitive_over: part_of ! part_of
输出:
$VAR1 = {
'GO:0000001' => [
'mitochondrion inheritance',
'biological_process'
],
'GO:0000002' => [
'mitochondrial genome maintenance',
'biological_process'
]
};
希望这有帮助!
答案 1 :(得分:3)
这是一个很有用的技巧。 Perl有一个$/
变量,用于定义“输入记录分隔符” - 当您使用<DATA>
读取输入记录时,它将一直读取,直到遇到设置为$/
的任何内容,并且然后返回所有数据。
通常$/
设置为换行符,因此<DATA>
一次从文件返回一行。但是如果你将它设置为空字符串""
,那么每次读取都将返回所有数据,直到下一个空行或一系列空行
$/ = "";
while (<DATA>) {
chomp; # remove the trailing newlines
# $_ now contains a whole blank-line-separated chunk
if (/^\[Term\]/) {
...
# parse the [Term] chunk here
...
}
}
在循环内部,您可以通过将块拆分成行来解析块,然后拆分:
字符串上的每一行以获取键和值。此时,您可以将该块的键和值放入您喜欢的任何类型的结构中。