• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【Perl】序列匹配

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

http://bbs.chinaunix.net/viewthread.php?tid=3621368&from=favorites

Unix技术网的牛人就是多啊~~~一

题目:

有文本文件含5条序列(每行一条)
GAGCATTCAGCAT
GACCATCCAGCTT
GACCATCCTGGTA
GACGATCTAGCAT
AGGCATCATGCAA
要求对这些序列两两对比  如果字符一样 就在下方标*号 以一二条序列为例,输出如下:
GAGCATTCAGCAT
GACCATCCAGCTT
**   ***  ****  *
The number of * =10
输出所有两两对比数据


1.

 1 chomp(@ARGV = <DATA>);
2 while ( my $str1 = shift)
3 {
4 for my $str2 ( @ARGV )
5 {
6 my $str3 = $str1 ^ $str2;
7 $str3 =~ s/[^\0]/ /g;
8 my $number = $str3 =~ s/\0/*/g;
9 print "$str1\n";
10 print "$str2\n";
11 print "$str3\n";
12 print "The number of * = $number\n\n";
13 }
14 }
15
16 __DATA__
17 1GAGCATTCAGCAT
18 2GACCATCCAGCTT
19 3GACCATCCTGGTA
20 4GACGATCTAGCAT
21 5AGGCATCATGCAA


  

2.

chomp(my @data=<DATA>);
for my $i(0..$#data-1)
{
my ($counter, $out);
for my $j ($i+1..$#data)
{
substr($data[$i],$_,1) eq substr($data[$j],$_,1) ? ($counter++,$out.="*") : ($out.="") for(0..length($data[$i])-1);
print $data[$i],"\n",$data[$j],"\n",$out,"\nThe number of * =",$counter,"\n\n";
($counter,$out)=();
}
}

__DATA__
1GAGCATTCAGCAT
2GACCATCCAGCTT
3GACCATCCTGGTA
4GACGATCTAGCAT
5AGGCATCATGCAA




3.

my @array=("1GAGCATTCAGCAT",
"2GACCATCCAGCTT",
"3GACCATCCTGGTA",
"4GACGATCTAGCAT",
"5AGGCATCATGCAA");
my $line1;
while (@array>0)
{
$line1=shift(@array);
foreach (@array)
{
print $line1,"\n",$_,"\n";
fun([split //,$line1],[split //,$_]);
}
}
sub fun{
my ($ref1,$ref2,$num,$out)=@_;
while (@{$ref1}>0)
{
if (shift(@{$ref1}) =~ shift(@{$ref2}))
{
$num++;
$out.='*';
} else
{
$out.='';
}
}
print $out,"\n","The number of '*' is $num\n\n";
}
__DATA__
1GAGCATTCAGCAT
2GACCATCCAGCTT
3GACCATCCTGGTA
4GACGATCTAGCAT
5AGGCATCATGCAA


 

4.

my @a = map {chomp;[split '']} <DATA>;
for my $x (0..@a-2)
{
for my $y ($x+1..@a-1)
{
my $s;
print @{$a[$x]}, "\n", @{$a[$y]},"\n";
print map{$a[$x][$_] eq $a[$y][$_] ? do {$s += 1;'*'} : ''} 0..$#{$a[$x]}-1;
print "\nThe number of * = $s\n\n";
}
}
__DATA__
1GAGCATTCAGCAT
2GACCATCCAGCTT
3GACCATCCTGGTA
4GACGATCTAGCAT
5AGGCATCATGCAA



 

5.

my $sLast = <DATA>;
while (<DATA>)
{
my $sOut = "";
my $sCnt = 0;
print "$sLast$_";
for my $sLoc (0 .. length($_) -2 )
{
if (substr($sLast,$sLoc,1) eq substr($_,$sLoc,1))
{
$sOut .="*";
$sCnt++;
} else
{
$sOut .="";
}
}
print "$sOut\n";
print "The number of * =$sCnt\n\n";
$sLast = $_;
}
__DATA__
1GAGCATTCAGCAT
2GACCATCCAGCTT
3GACCATCCTGGTA
4GACGATCTAGCAT
5AGGCATCATGCAA




6.

my @a = map {chomp;[split '']} <DATA>;
for my $x (0..@a-2)
{
for my $y ($x+1..@a-1)
{
my $s;
print @{$a[$x]}, "\n", @{$a[$y]},"\n";
print map{$a[$x][$_] eq $a[$y][$_] ? do {$s += 1;'*'} : ''} 0..$#{$a[$x]}-1;
print "\nThe number of * = $s\n\n";
}
}
1GAGCATTCAGCAT
2GACCATCCAGCTT
3GACCATCCTGGTA
4GACGATCTAGCAT
5AGGCATCATGCAA






鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
为ZendStudio配置Perl支持发布时间:2022-07-22
下一篇:
perlDBI的railseerror用法发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap