我有两个文件。一个由唯一的列表组成,而另一个是带有年龄的冗余列表。
例如
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";