Re: [問題] 合併三個檔案中的資料

看板Perl作者 (markwong)時間15年前 (2010/05/04 04:12), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《markwong (markwong)》之銘言: : ※ 引述《adu (^_^)》之銘言: : : FILE1: : : name1,chrA1 : : name2,chrA2 : : name3,chrA3 : : ... : : FILE2: : : name1,chrB1 : : name2,chrB2 : : name3,chrB3 : : ... : : FILE3: : : name1,chrC1 : : name2,chrC2 : : name3,chrC3 : : ... : : 要將output變成: : : name1,chrA1,chrB1,chrC1 : : name2,chrA2,chrB2,chrC2 : : name3,chrA3,chrB3,chrC3 : : ... : : 每個file都有20萬筆 : : 我現在是用while( : : while( : : while( : : ) : : ) : : ) : : 的方式比對,很沒效率而且會out of memory XD : : 並附加error "Use of uninitialized value $Name1 in string" : : 請教板大們有沒有比較好的方法呢? : : 謝謝板大:) : : 附上我的script: : : ##!/usr/bin/perl : : use strict; : : use warnings; : : my $file1 = "A.txt"; : : open FILE1, $file1 or die "File open error!!"; : : my $fileOUT = ">OUTPUT.txt"; : : open FILE0, $fileOUT or die "File open error!!"; : : while(<FILE1>){ : : chomp($_); : : my $Line1 = $_; : : if($Line1 =~ /(.+?),(.+)/){ : : my $Name1 = $1; : : my $CHR1 = $2; : : my $file2 = "B.txt"; : : open FILE2, $file2 or die "File open error!!"; : : while(<FILE2>){ : : chomp($_); : : my $Line2 = $_; : : if($Line2 =~ /(.+?),(.+)/){ : : my $Name2 = $1; : : my $CHR2 = $2; : : if($Name1 eq $Name2){ : : my $file3 = "C.txt"; : : open FILE3, $file3 or die "File open error!!"; : : while(<FILE3>){ : : chomp($_); : : my $Line3 = $_; : : if($Line3 =~ /(.+?),(.+)/){ : : my $Name3 = $1; : : my $CHR3 = $2; : : if($Name1 eq $Name3){ : : print FILE0 "$Name3,$CHR1,$CHR2,$CHR3\n"; : : } : : } : : } : : } : : } : : } : : } : : } : This might be a quick and dirty way. : #!/usr/bin/env perl : use strict; : use warnings; : # assuming your files are file1.txt, file2.txt and file3.txt. : push @ARGV, qw( file1.txt file2.txt file3.txt ); : my %result; : # the diamond will read the files from @ARGV list. : while(<>) { : chomp; : my ($key, $value) = /(.+),(.+)/; : # assuming your name1, name2, ..., and nameX are unique. we can put it : into a hash. : $result{$key} .= ",$value"; : } : # now we iterate the hash and generate output. : open my $output, ">OUTPUT" or die "Cannot open OUTPUT file: $!\n"; : foreach my $key (sort keys %result) { : print $output "${key}$result{$key}\n"; : } : close $output; This is a much better version. Please read 'perlreftut' for the idea. #!/usr/bin/env perl use strict; use warnings; # # you may want to read 'perlreftut' in order to get the idea. # # Assume that your input files are file1.txt file2.txt and file3.txt push @ARGV, qw( file1.txt file2.txt file3.txt ); # we are using a hash here. it will contain a key # and the value of particular key is a list reference. # eg. { # 'name1' => ['chrA1', 'chrA2', 'chrA3'], # 'name2' => ['chrB1', 'chrB2', 'chrB3'], # 'name3' => ['chrC1', 'chrC2', 'chrC3'], # } my %result; while (<>) { chomp; my ($key, $value) = split ','; push @{$result{$key}}, $value; } open my $output, '>OUTPUT' or die "Cannot open OUTPUT file: $!\n"; foreach my $key (sort keys %result) { print $output "$key," . join(',', sort @{$result{$key}}) . "\n"; } close $output; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 115.133.239.181

05/04 17:16, , 1F
謝謝m大! 我之前在做調整,遲遲沒有回應還請見諒:D
05/04 17:16, 1F
文章代碼(AID): #1Btov9Xc (Perl)
文章代碼(AID): #1Btov9Xc (Perl)