如何去掉全为0的行?

如何去掉全为0的行?

0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 9 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 3 0 5 0 0 8 0 0
0 0 2 3 0 5 0 0 8 0 10

将全为0行去掉后
然后找出完全相同的行
即0 0 2 3 0 5 0 0 8 0 0 行共出现3次
子曰:不愤不启,不悱不发,举一隅不以三隅反,则不复也。
怎样把全为0的行和全为0的元素全部去掉?
己最后得到的结果是三行
2 3  5  8
2 3  5  8
2 3  5  8
9 10为什么不留下?


[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
use strict;
use warnings;

my $a="D:/personal/1.txt";
open(FILE,"$a")||die;
while (my $line=<FILE>){
$line=~s/0\s//g;
unless ($line=~m/^\s*$/){
print "$line\n";
}
}
close FILE;



[Copy to clipboard] [ - ]
CODE:
1.txt:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 9 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 3 0 5 0 0 8 0 0
0 0 2 3 0 5 0 0 8 0 10

非全0的行

[Copy to clipboard] [ - ]
CODE:
my $a="test.txt";
open(FILE,"$a")||die;
while (my $line=<FILE>){
unless($line=~/[0\s]{10}0$/){
  print $line;
}
}
close FILE;

test.txt

[Copy to clipboard] [ - ]
CODE:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 5 0 0 8 9 0
0 0 0 0 0 0 0 0 0 0 0
0 0 2 3 0 5 0 0 8 0 0
0 0 2 3 0 5 0 0 8 0 10



QUOTE:
原帖由 royalzhang 于 2007-9-23 22:53 发表
子曰:不愤不启,不悱不发,举一隅不以三隅反,则不复也。


lz和大家要细细品位 先生 的话啊
做练习的话,自己下去玩玩就行了
print "input filename \n";
chomp(my $zero=<>);
open(FIL,$zero) or die;
my %count;
while(<FIL>){
chomp;
unless(/^\s*0(\s0)+$/){
$count{$_}++;
  }
}
print map{$_,"==>".$count{$_}."\n"} keys %count;

OK...如不想匹配开头有空白,可以见\s*去了
测试结果如下
C:\>perl test.pl
input filename
delzero.txt
0 0 2 3 0 5 0 0 8 0 10==>1
0 0 2 0 0 5 0 0 8 9 0==>1
0 0 2 0 0 5 0 0 8 0 0==>2
0 0 2 3 0 5 0 0 8 0 0==>1