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