2012-10-26 28 views
-2

我有這樣的情況:我有一個文件1匹配不同的文件列

文件1

List  ID 
1   NM_00012 
2   NM_00013 
2   NM_00013 
3   NM_00021 
3   NM_00021 
4   NM_000254 
5   NM_000765 

一個文件,看起來像這樣第二個文件:

文件2

List  Count 
1   Gene1 
2   Gene2 
2   Gene2 
3   Gene3 
3   Gene3 
4   Gene4 
5   Gene5 

我想輸出如下:

文件3

List  Count 
NM_00012 Gene1  
NM_00013 Gene2   
NM_00021 Gene3     
NM_000254 Gene4   
NM_000756 Gene5   

誰能幫助我? 我完全是Perl新手。

在此先感謝!

+1

堆棧溢出的目的是幫助誰想盡辦法,並不能找到程序員一個辦法。你似乎沒有做任何努力。請說明你的嘗試,並解釋你的具體問題是什麼。如果你對Perl完全陌生,那麼我建議你從教程開始 – Borodin

+1

是的,我完全同意你的看法,但我現在必須解決這個問題!我等不及了。 – Bnf8

+0

然後,你需要僱用一名程序員。這不是免費完成工作的地方 – Borodin

回答

0

您可以像(未測試):

my (%hash1, %hash2, $list, $count, $ID); 
open F,"<","file2.txt" or die; 
while(<F>) { 
    chomp; 
    ($list,$count) = split/\s+/; 
    $hash1{$list} = $count; 
} 
close F; 

open F,"<","file1.txt" or die; 
while(<F>) { 
    chomp; 
    ($list,$ID) = split/\s+/; 
    if(! exits $hash2{$ID}) { 
    print "$ID $hash1{$list}"; 
    $hash2{$ID} = 1; 
    } 
} 
close F; 
+0

非常感謝代碼上癮! – Bnf8

1

那麼,有幼稚和簡單的實現:

open FILE1, "file1.txt"; 
open FILE2, "file2.txt"; 
open OUTPUT, ">", "output.txt"; 

my (%file1content, %file2content); 

%file1content = ProcessFile(\*FILE1); 
%file2content = ProcessFile(\*FILE2); 

sub ProcessFile { 
my (%ret, @arr); 
my $fh = shift; 
while (@arr = split(/[\s\t]+/,<$fh>)) { 
next unless(scalar(@arr) == 2); 
next unless(($arr[0]+0) > 0); 
$ret{$arr[0]} = $arr[1]; 
} 
return %ret; 
} 

foreach my $key (sort {$a cmp $b} keys %file1content){ 
print OUTPUT $file1content{$key},"\t",$file2content{$key},"\n"; 
} 
close (OUTPUT); 
close (FILE1); 
close (FILE2); 
+0

非常感謝您羅勒的幫助! – Bnf8