- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2014-08-12 21:36 编辑
貌似设置几个变量记录上一行的信息和start number就可以了,这样行不行{:3_193:}- #!perl
- use Modern::Perl;
- use FileHandle;
- my $your_data_file = 'a.txt';
- my $fh = FileHandle->new($your_data_file, 'r');
- my $start_num = q{};
- my $last_line = [ q{}, q{}, 0, q{}, q{}, q{} ];
- my $count = 0;
- while (my $line = $fh->getline) {
- # remove trailing newline
- chomp $line;
- # get columns of current line
- my $current_line = [(split /\s+/, $line)];
- # compare currentline with last line
- if ($current_line->[2] == $last_line->[2] + 1) { # number increase by one
-
- if ($last_line->[1] eq $current_line->[1] and
- $last_line->[4] eq $current_line->[4] and
- $last_line->[5] eq $current_line->[5]) { # column 2, 5, 6 are equal
- # valid consecutive row, note it
- ++$count;
- }
- else {
- # column 2, 5, 6 not equal
- # the first line doesn't count, $count equal 2 suffice
- if ($count >= 2) {
- my @copy = @{ $last_line };
- $copy[2] = "$start_num-$copy[2]";
- say join "\t", @copy;
- }
- # reset
- $start_num = $current_line->[2];
- $count = 0;
- }
- }
- else {
- # number not consecutive
- if ($count >= 2) {
- my @copy = @{ $last_line };
- $copy[2] = "$start_num-$copy[2]";
- say join "\t", @copy;
- }
- # reset
- $start_num = $current_line->[2];
- $count = 0;
- }
- $last_line = $current_line;
- }
- $fh->close;
- __END__
复制代码 |
|