# File: matchHash1.pl # Title: Take all groups of n chars from a string (non-overlapping) # $s =~ m/(.{$n})/g; # means match(m) first n chars .{$n} - that is repeat . n times # () means set result in variable $1 # g - globally will find all solutions by turn # and put them in a hash (array with index as string) # Author: Mihaela Malita # Input string? abcxxxabcyyyabc # Input integer? 3 # Hash is pairs of kmers of size 3 with their last position # abc 12 # xxx 3 # yyy 9 print "Input string? "; $s = ; chop($s); print "Input integer? "; $k = ; chop($k); %groupHash = (); # initialize hash (string-index) $i = 0; # initialize index- how many groups $pos = 0; # position while ( $s =~ m/(.{$k})/g ) { # each time variable $1 takes the value of the match $groupHash{$1} = $pos; # value = the number of groups $i++; # next group $pos = $k * $i; # position is how many groups multiplied with their size } print("Hash is pairs of kmers of size $k with their last position\n"); printHash( %groupHash); exit; sub printHash { # prints a hash key-value on each line my(%D) = @_; foreach $key (keys %D) { # keys are the strings print $key, "\t", $D{$key},"\n"; } return; }