私は2つのファイルを持っています。1 つは一意のリストで構成され、もう 1 つは名前と年齢の重複リストです。
例えば
File1: File2:
Gaia Gaia 3
Matt Matt 12
Jane Gaia 89
Reuben 4
私の目的は、File1 と File2 を一致させ、それぞれの名前の最高年齢を取得することです。これまでのところ、以下のコードを書いています。あまりうまくいかないのは、ハッシュで同じキーが見つかった場合、より大きな値を出力することです。
提案/コメントは大歓迎です!
ありがとう!!
#!/usr/bin/perl -w
use strict;
open (FILE1, $ARGV[0] )|| die "unable to open arg1\n"; #Opens first file for comparison
open (FILE2, $ARGV[1])|| die "unable to open arg2\n"; #2nd for comparison
my @not_red = <FILE1>;
my @exonslength = <FILE2>;
#2) Produce an Hash of File2. If the key is already in the hash, keep the couple key- value with the highest value. Otherwise, next.
my %hash_doc2;
my @split_exons;
my $key;
my $value;
foreach my $line (@exonslength) {
@split_exons = split "\t", $line;
@hash_doc2 {$split_exons[0]} = ($split_exons[1]);
if (exists $hash_doc2{$split_exons[0]}) {
if ( $hash_doc2{$split_exons[0]} > values %hash_doc2) {
$hash_doc2{$split_exons[0]} = ($split_exons[1]);
} else {next;}
}
}
#3) grep the non redundant list of gene from the hash with the corresponding value
my @a = grep (@not_red,%hash_doc2);
print "@a\n";