# File: searchAllsub.pl # Title: Given string and a character (pattern) # Capture all the positions of the character in string # use system function: pos($seq) - gives next position # C:\myPerl>perl searchAllsub.pl # There are 3 positions where a is found in mathematica ### Find positions for a in mathematica *** $seq = 'mathematica'; $regex = 'a'; #search a in mathematica @Places = searchAll($seq,$regex); #call subroutine $Places = @Places; # find length array result print "\nThere are ", $Places, " positions where $regex is found in $seq: ", @Places; exit; ### Subroutine, takes 2 args: sequence and a charcater and return list of positions ### sub searchAll { my @Positions = (); # initialize array where you put positions my ($s,$r) = @_; # array of arguments from main program: sequence and character while ( $s =~ m/$r/g ) { # search globally (g) pattern $r is $s push (@Positions,(pos($s)-1)); # decrement because pos() gives next position } return @Positions; }