in reply to check for contiguous row values

This seems to work:

#!/usr/bin/perl use warnings; use strict; open my $IN, '<', 'input.txt' or die "Can't open 'input.txt' $!"; open my $FILE, '>', 'output.txt' or die "Can't open 'output.txt' $!"; my $previous = -1; my @data; while ( <$IN> ) { my ( $n, $p ) = split; no warnings qw/ numeric uninitialized /; if ( $n == $previous + 1 ) { push @data, $p } else { print $FILE "$data[0]\t$data[-1]\t", scalar @data, "\n +" if @data > 1; @data = $p; } $previous = $n; } __END__

Replies are listed 'Best First'.
Re^2: check for contiguous row values
by kennethk (Abbot) on Aug 31, 2010 at 20:26 UTC
    Not quite - you exit before outputting the final line. You need to add another line to flush @data, e.g.:

    print $FILE "$data[0]\t$data[-1]\t", scalar @data, "\n" if @data > 1;

    after the while loop. This depends on how your file is terminated - your code will function correctly if it is terminated with two or more newlines.