【发布时间】:2017-03-03 13:00:33
【问题描述】:
我有一个类似下面的文件
1 B B C D B
2 K B D D K
1 B B C D B
2 K B D D K
1 B B C D B
2 K B D D K
我希望输出看起来像这样
1 B C D
2 K B D
1 B C D
2 K B D
1 B C D
2 K B D
排序命令不起作用,所以我尝试编写一个 Perl 程序 -
use strict;
use Data::Dumper;
my $file1 = <$ARGV[0]>;
open (IF2, "$file1") || die "Cannot open the file\n";
open (OUT, ">$file1.out") || die "Cannot open the out file\n";
my $k = 0;my $i=0;
my @line;
my $m;
my @line2;
while ( chomp($m = <IF2>) ) {
my $count = 0;
@line2 = split(/\t/,$m);#<stdin>;
my $l = length @line2;print $l;<stdin>;
for (my $x = 0; $x < $l;$x++) {
my $k = 0;
for (my $y = 0;$y < $l; $y) {
$i++;
#
if ($count == 0)
{
print OUT "\t$line2[$x]";
$count++;
}
if ($count != 0 && $x != $y)
{
if ($line2[$x] eq $line2[$y])
{
$k++;
}
}
}
if ($k == 0)
{
print OUT "\t$line2[$x]";
}
}
print OUT "\n";
}
print $i;
close IF2;
close OUT;
但它没有用。 有人可以帮忙吗?
【问题讨论】:
-
这非常非常复杂。您可以使用 Perl,有多种处理文本的方法,所以请编写 Perl,而不是 C。当您阅读该行时,split 它,然后通过
uniq从 List::Utils 模块传递该列表 -
等等......你不想要“uniq”元素——你的输出有多个
Bs、Ds等。你只想删除相邻重复,对吗?如果是这样,我上面的评论是不对的,uniq只会留下一个B,一个D,等等