问题描述
我有两个文件1.txt和2.txt。
这两个文件的结构是:
Main_File1开始结束
1 200 250
2 310 340
Main_File2开始结束
1 200 250
2 350 370
我想编写一个perl代码,其输出应该是两个文件,即一个文件具有常见的开始和结束位置的列表以及具有唯一的开始和结束位置的其他文件。
>设计部分提供了一个对每个开始,结束对唯一的密钥,但并不难。因为开始和结束都是数字,所以这应该很简单:
我们的%matchups;
子过程
{
my($ lst _)= @_; (@ $ lst_){
my($ strt,$ endn)= / \ d + \w +(\ d +)\w +(\ d +)/;
。
next除非$ strt&& $ endn;
my $ key =$ {strt} _ $ endn;
$ matchups {$ key} [0] = $ _;
$ matchups {$ key} [1] ++;
}
}
子输出匹配
{
my($ dest,$ multi)= @_; ($ _-> [1]> 1)== $
$打开文件
(值%matchups){
print $ OUT $ _-> [0]多
}
}
{
process(@ listfrom1txt);
进程(@ listfrom2txt);
outputmatch(common.txt,1);
outputmatch(uniq.txt,0);
$ b 所以在这里我们做一个关键是 start_end
,然后我们在散列内部构建一个数据结构,这是一个由两个元素组成的数组。第一个元素是原始行,第二个元素是我们看过这个条目的次数。
如果一行是唯一的,则计数将为 1
;如果不是,那么它将大于 1
。
I have two files "1.txt" and "2.txt".
The structure of both the files is :
Main_File1 Start End
1 200 250
2 310 340
Main_File2 Start End
1 200 250
2 350 370
I want to write a perl code for which the output should be two files i.e. one file having the list of common "start" and "end" positions and other file having unique "start" and "end" positions.
解决方案 I'm going to guess you already have code for reading each of these files, and just need to parse out the values and organize them.
The design part is coming up with a key that is unique for each start,end pair, but not hard to work with. Since start and end are numbers, this should be pretty easy:
our %matchups ;
sub process
{
my ($lst_)= @_ ;
for ( @$lst_ ) {
my ($strt,$endn)= /\d+\w+(\d+)\w+(\d+)/ ;
next unless $strt && $endn ;
my $key= "${strt}_$endn" ;
$matchups{$key}[0]= $_ ;
$matchups{$key}[1] ++ ;
}
}
sub outputmatch
{
my ($dest,$multi)= @_ ;
# open file
for ( values %matchups ) {
print $OUT $_->[0] if ( $_->[1] > 1 ) == $multi ;
}
}
{
process(@listfrom1txt) ;
process(@listfrom2txt) ;
outputmatch( "common.txt", 1 ) ;
outputmatch( "uniq.txt", 0 ) ;
}
So here we make a key which is start_end
, and then we build a data structure inside the hash which is an array of two elements. The first element is the original line, the second is the count of how many times we've seen this entry.
If a line is unique, the count will be 1
; if it's not, then it will be greater than 1
.
这篇关于两个文件中的Perl列比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!