my %endpoints; $endpoints{ $-[0] - 1 } = $+[0] - $-[0] - 1 while $chromosome =~ m[(CG.*?AT)]g; ... while( my( $begin, $end ) = each %exon_endpoints ) { print substr( $chromosome, $begin, $end ), "\n\n"; }