in reply to find the continuity in the data file
Change $limit to 2500 for your real data (and direction to -1 if you want continuous blocks of descending numbers).my @nums=qw(16 107 108 110 112 114 115 117 118 119 120 121 122 123 124 + 125 127 128 130 132 133 135 136 138 142 146 149 150 154 156 157 158 +159 161 162); my $limit=4; my $direction=1; my @cont=get_cont($limit,$direction,@nums); if(@cont>0) { print "First continuous block of $limit numbers in ",$direction>0? +"ascending":"descending"," direction is: ",join(',',@cont); } else { print "No block was found.\n"; } sub get_cont { my $limit=shift; my $dir=shift; my @nums=@_; my $prev; my @cont; for $num (@nums) { if(($num-$prev)==$dir) { push(@cont,$num); if(@cont>=$limit) { return @cont; } } else { @cont=(); } $prev=$num; } return (); }
|
|---|