分塊數據的元素我有一個看起來像這樣的數據:獲取使用Perl
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"];
我該如何去做Perl?
我堅持用這個代碼:
#!/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
測試在這裏:https://eval.in/80497