use warnings; use strict; open GENE, '<', '\test.txt' or die("Unable to read: $!"); my @rolling = (undef, undef, undef); my %count; my $cnt; until (eof GENE) { my $char; read(GENE,$char,1); next unless $char=~/[AGCT]/; #make sure it's a valid char; shift @rolling; push @rolling, $char; next unless defined $rolling[2]; $count{$rolling[2]}++; #one-char count next unless defined $rolling[1]; $count{join('',@rolling[1,2])}++; #two-char count next unless defined $rolling[0]; $count{join('',@rolling)}++; #three-char count }