-1

我正在使用数组中的 foreach 循环获取文件的输出。

获取所有内容后,如何仅将文件的一部分提取到新数组中。

这是我的代码

foreach (@genetic_codes) {
  chomp;
  my @genetic_codes = split(':', $_);
  if (@genetic_codes != 5) { # error on this line next;
  }
  my $amino_acid = join('","', split(/,/, $genetic_codes[4]));
  print "$genetic_codes[2]=> [$genetic_codes[0],$genetic_codes[1],[$amino_acid]],\n";
}

这是我的正确输出。

"M"=> ["Methionine","Met",["ATG"]],
"F"=> ["Phenylalanine","Phe",["TTT"," TTC"]],
"P"=> ["Proline","Pro",["CCT"," CCC"," CCA"," CCG"]],
"S"=> ["Serine","Ser",["TCT"," TCC"," TCA"," TCG"," AGT"," AGC"]],
"T"=> ["Threonine","Thr",["ACT"," ACC"," ACA"," ACG"]],
"W"=> ["Tryptophan","Trp",["TGG"]],

现在我需要获取所有密码子并将它们放入一个变量Z中以消除重复项。

我需要单独制作一个foreach循环吗?

我完全迷路了,请帮助。我需要我的最后一个输出----

"Z"=>["ACT","AGT",---------------SO ON]],

上面一行的所有三个字母都在一个变量中。

4

1 回答 1

0

您需要更改代码,以便将密码子存储在每个输出行的单独数组中。然后,您可以逐行建立哈希。

我还修复了输入的处理,以便您的数据正确。

由于您没有提供任何示例输入数据,因此我制作了一些我认为 k 是正确的东西,并产生了您在问题中显示的输出。

use strict;
use warnings;

my %codons;

while (<DATA>) {
  chomp;
  my @genetic_codes = split /:/;
  @genetic_codes == 5 or die "Invalid data found";
  my @amino_acids = $genetic_codes[4] =~ /[ACTG]+/g;
  printf "%s => [%s, %s, [%s]],\n",
      @genetic_codes[2, 0, 1],
      join ', ', map qq{"$_"}, @amino_acids;#
  $codons{$_}++ for @amino_acids;
}
printf qq{"%s" => [%s]\n}, 'Z', join ', ', map qq{"$_"}, sort keys %codons;

__DATA__
"Methionine":"Met":"M":"":"ATG"
"Phenylalanine":"Phe":"F":"":"TTT, TTC"
"Proline":"Pro":"P":"":"CCT, CCC, CCA, CCG"
"Serine":"Ser":"S":"":"TCT, TCC, TCA, TCG, AGT, AGC"
"Threonine":"Thr":"T":"":"ACT, ACC, ACA, ACG"
"Tryptophan":"Trp":"W":"":"TGG"

输出

"M" => ["Methionine", "Met", ["ATG"]],
"F" => ["Phenylalanine", "Phe", ["TTT", "TTC"]],
"P" => ["Proline", "Pro", ["CCT", "CCC", "CCA", "CCG"]],
"S" => ["Serine", "Ser", ["TCT", "TCC", "TCA", "TCG", "AGT", "AGC"]],
"T" => ["Threonine", "Thr", ["ACT", "ACC", "ACA", "ACG"]],
"W" => ["Tryptophan", "Trp", ["TGG"]],
"Z" => ["ACA", "ACC", "ACG", "ACT", "AGC", "AGT", "ATG", "CCA", "CCC", "CCG", "CCT", "TCA", "TCC", "TCG", "TCT", "TGG", "TTC", "TTT"]
于 2013-02-23T18:53:44.390 回答