G
Graham Drabble
I have a file that contains a list of numbers. I'm trying to process
the file to find out how many rows start with 4 consecutive numbers
(either ascending or decending). Currently I've got
use strict;
use warnings;
open (IN, '4bell.txt') or die "Can't open IN: $!";
my $runs = 0;
while(<IN>){
chomp;
my $first = substr($_,0,1);
my $asc = $first . $first+1 . $first+2 . $first+3;
my $des = $first . $first-1 . $first-2 . $first-3;
if (/^($asc|$des)/){
$runs++
}
}
print "There were $runs runs\n";
IN
12345867
23457658
34568765
43215687
13245678
Prints
There were 4 runs
which is correct. However I can't help but think there must be a
shorter solution but can't think of it. Any ideas? The file could
contain up to 5000 lines.
the file to find out how many rows start with 4 consecutive numbers
(either ascending or decending). Currently I've got
use strict;
use warnings;
open (IN, '4bell.txt') or die "Can't open IN: $!";
my $runs = 0;
while(<IN>){
chomp;
my $first = substr($_,0,1);
my $asc = $first . $first+1 . $first+2 . $first+3;
my $des = $first . $first-1 . $first-2 . $first-3;
if (/^($asc|$des)/){
$runs++
}
}
print "There were $runs runs\n";
IN
12345867
23457658
34568765
43215687
13245678
Prints
There were 4 runs
which is correct. However I can't help but think there must be a
shorter solution but can't think of it. Any ideas? The file could
contain up to 5000 lines.