This is not quite a trivial task. There is also debate whether providing a finished program is helpful for others learning to solve a problem in a programming language, but I believe it has its merits, so I propose the following program (let's call it findlongestsequence.pl
:
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $limit; my $infile;
GetOptions( 'limit=f' => \$limit, 'infile=s' => \$infile );
my $lineno=0; my $groupstart;
my $currlength=0; my $maxlength=0; my $ingroup=0;
my @columns; my @groupbuf; my @longestgroup;
if (! open(fileinput, '<', "$infile" )) {exit 1;};
while (<fileinput>)
{
$lineno++;
@columns = split(/\s+/,$_);
if ( $ingroup == 0 && $columns[1]<$limit )
{
$ingroup=1;
$groupstart=$lineno;
@groupbuf=();
}
if ( $ingroup == 1 )
{
if ($columns[1]>=$limit )
{
$ingroup=0;
$currlength=$lineno-$groupstart;
if ( $currlength>$maxlength )
{
$maxlength=$currlength;
@longestgroup=@groupbuf;
}
}
else
{
push(@groupbuf,$columns[1]);
}
}
}
close(fileinput);
if ( $ingroup == 1 )
{
$currlength=$groupstart-$lineno;
if ( $currlength>$maxlength )
{
$maxlength=$currlength;
@longestgroup=@groupbuf;
}
}
print join("\n",@longestgroup),"\n";
exit 0;
You can call the program as
./findlongestsequence.pl --infile input.txt --limit 2.0
This will first interpret the command-line parameters using Getopt::Long
.
It will then open the file and read it line-wise while, and keep a line-counter in $lineno
. Every line will be split into columns at whitespace.
- If the program is not inside a group of lines with values <
$limit
($ingroup
is zero), but encounters a suitable line, it will record that it is now in such a group ($ingroup
set to one), store the group start in $groupstart
and start buffering the column 2 values in an array @groupbuf
.
- If the program is inside such a group, but the current value is larger than the
$limit
, it will recognize the end-of-group and calculate its length. If this is longer than the previously recorded longest group, the content (@groupbuf
) and length ($currlength
) of the new longest group is copied to @longestgroup
and $maxlength
, respectively.
Since it is possible that a group is terminated by end-of-file rather than a line with value > $limit
, perform this check also if $ingroup
is true at end-of-file.
At the end, the content of @longestgroup
is printed with \n
as token separator.