Easy job for a regex:
while ($string =~ /((\w+)\w?(??{reverse $2}))/g) { print "$1\n"; } while ($string =~ /((\w+)\w?(??{reverse $2}))/g) { print "$1\n"; } text.scan(/(.)(.)(.)(.)(\3)(\2)(\1)/)Oddly enough, it didn't even occur to me to try a regex and I like using them quite a bit.