use strict; use warnings; my ( $length, @lengths ); my $last = 0; while ( ) { next unless /Z\s+(\d+)\s+(\d+)/ and $2 >= $1; if ( $1 > $last + 1 ) { push @lengths, $length if $length; $length = $2 - $1 + 1; } else { $length += $2 - $1 + 1; } $last = $2; } push @lengths, $length; print scalar @lengths, " Z items found:\n"; print join "\n", @lengths; __DATA__ Z 1 1 Z 5 89 ~#à^''$%!@ => line noise Z 91 102 Z 103 123 Z 124 150 Z 151 191 Z 500 504 Z 505 509