# File: searchAll.pl # Title: Given string and a substring (pattern) # Capture all the positions of the substring in string # use system function pos($seq) - gives next position # C:\myPerl>perl searchAll.pl # $& = a $` = m$' = thematica # Position of the pattern is: 2 # $& = a $` = mathem$' = tica # Position of the pattern is: 7 # $& = a $` = mathematic$' = # Position of the pattern is: 11 # All positions where a is found in mathematica: 1610 ### Find positions for a in mathematica *** $seq = 'mathematica'; @positions = (); # initialize array where you put positions while ( $seq =~ m/a/g ) { # search globally (g) ;print '$& = ', "$&\t"; # string before $` ;print '$` = ', "$`"; # string $& ;print '$\' = ', "$'\t"; # string after $' print "\nPosition of the pattern is: ", pos($seq),"\n"; push (@positions,(pos($seq)-1)); # decrement because you have next position } print "\nAll positions where a is found in $seq: ", @positions; exit;