my $input = 'fragments.txt'; my %split; open(my $infile, '<', $input) or die "Cannot open file '$input' for reading: $!"; build_hash($_) while <$infile>; close $infile or die "Cannot close file '$input': $!"; build_hash(length($sequence) + 1); sub build_hash { use feature 'state'; state $old = 0; state $cnt = 1; my ($line) = @_; my $gap = $line - $old; my $start = $old + 1; my $end = ($gap > 2) ? $line - 1 : $start; $split{$cnt++} = [$start .. $end] if $gap >= 2; $old = $line; }