我是Perl的新手,必须编写一个代码,将文件内容和数组一起打印输出,看起来像哈希。这是一个示例条目:
my %amino_acids = (F => ["Phenylalanine", "Phe", ["TTT", "TTC"]])
输出应该完全采用上述格式。
文件行是这样的......
"Methionine":"Met":"M":"AUG":"ATG"
"Phenylalanine":"Phe":"F":"UUU, UUC":"TTT, TTC"
"Proline":"Pro":"P":"CCU, CCC, CCA, CCG":"CCT, CCC, CCA, CCG"
我必须在分号后取最后一个密码子并忽略第一组。
答案 0 :(得分:3)
您打算构建等效哈希吗?或者你真的想要字符串格式吗?该程序使用Text::CSV
从文件构建哈希,然后使用Data::Dump
转储它,以便您也拥有字符串格式。
use strict;
use warnings;
use Text::CSV;
use Data::Dump 'dump';
my $csv = Text::CSV->new({ sep_char => ':' });
open my $fh, '<', 'amino.txt' or die $!;
my %amino_acids;
while (my $data= $csv->getline($fh)) {
$amino_acids{$data->[2]} = [
$data->[0],
$data->[1],
[ $data->[4] =~ /[A-Z]+/g ]
];
}
print '$amino_acids = ', dump \%amino_acids;
<强>输出强>
$amino_acids = {
F => ["Phenylalanine", "Phe", ["TTT", "TTC"]],
M => ["Methionine", "Met", ["ATG"]],
P => ["Proline", "Pro", ["CCT", "CCC", "CCA", "CCG"]],
}
<强>更新强>
如果您确实不想安装模块(这是一个非常简单的过程并使代码更加简洁和可靠),那么这就是您所需要的。
use strict;
use warnings;
open my $fh, '<', 'amino.txt' or die $!;
print "my %amino_acids = (\n";
while (<$fh>) {
chomp;
my @data = /[^:"]+/g;
my @codons = $data[4] =~ /[A-Z]+/g;
printf qq{ %s => ["%s", "%s", [%s]],\n},
@data[2,0,1],
join ', ', map qq{"$_"}, @codons;
}
print ")\n";
<强>输出强>
my %amino_acids = (
M => ["Methionine", "Met", ["ATG"]],
F => ["Phenylalanine", "Phe", ["TTT", "TTC"]],
P => ["Proline", "Pro", ["CCT", "CCC", "CCA", "CCG"]],
)
答案 1 :(得分:0)
假设您确实希望将有效的perl作为输出,这将执行此操作:
open(my $IN, "<input.txt") or die $!;
while(<$IN>){
chomp;
my @tmp = split(':',$_);
if(@tmp != 5){
# error on this line
next;
}
my $group = join('","',split(/,\s*/,$tmp[4]));
print "\$amino_acids{$tmp[2]} = [$tmp[0],$tmp[1],[$group]];\n";
}
close $IN;
使用您的样本行,输出为:
$amino_acids{"M"} = ["Methionine","Met",["ATG"]];
$amino_acids{"F"} = ["Phenylalanine","Phe",["TTT","TTC"]];
$amino_acids{"P"} = ["Proline","Pro",["CCT","CCC","CCA","CCG"]];
答案 2 :(得分:0)
@Borodin非常感谢您的回答,实际上我不必使用Text :: csv或Data :: dump.I必须打开文件并从文件构建等效哈希。我正在尝试不要同时使用它们,希望它会有所帮助。再次感谢!!!
答案 3 :(得分:-1)
Perl没有特殊的方法来打印哈希值。您应该做的是在读取文件时创建哈希:
while (<FILE>) {
my @line = split ':'; # split the line into an array
$amino_acids{$line[0]} = \@line[1..-1]; # take elements 1..end
}
然后一次打印出一个条目:
foreach (keys %amino_acids) {
print "$_ => [", (join ",", @$amino_acids{$_}), "]\n";
}
请注意,我没有对此进行编译,因此可能需要少量工作才能完成。