# File: search2.pl # Title: Input from keyboard a string and a regular expression (pattern) # Display all the positions of the pattern # Author: Mihaela Malita # Input string? pascal # Input pattern? a # $& = a $` = p $' = scal # Position of the pattern is: 2 # $& = a $` = pasc $' = l # Position of the pattern is: 5 # All positions of the pattern are: 25 print "Input string? "; $s = ; chop($s); print "Input pattern? "; $p = ; chop($p); @positions = (); # initialize array while ( $s =~ /$p/g ) { print '$& = ', "$&\t"; print '$` = ', "$`"; print '$\' = ', "$'\t"; print "\nPosition of the pattern is: ", pos($s),"\n"; push (@positions,pos($s)); } print "\nAll positions of the pattern are: ", @positions; exit;