# File: matchHash.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 # Groups and the number of groups n string # abc 4 # xxx 1 - counting starts from 0 # yyy 3 - third group print "Input string? "; $s = ; chop($s); print "Input integer? "; $n = ; chop($n); %groupHash = (); # initialize hash (string-index) $i = 0; # initialize index- how many groups while ( $s =~ m/(.{$n})/g ) { # each time variable $1 takes the value of the match $groupHash{$1} = $i; # value = the number of groups $i++; } print("Groups and the number of groups n string"); 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; }