#!/usr/bin/perl -w
#################
# find words with double letters.
# Example done in class on 11/28.
#
#####################
use strict;

my $string = "My father the bookkeeper likes red balloons.  So there.";
print "The string is '$string'\n";

# This finds all words with paired o's.
# The "x" switch means "extended syntax - embedded spaces, comments are OK
# The "g" switch means "global" - find all matches.
# In array context, the match returns a list of the matches.
my @doubleOs = $string =~ m{
			    \b      # word boundry at start of word
			    \w*     # any number of word characters
			    oo      # two o's
			    \w*     # more word characters
			    \b      # end of word
                           }gx;
print "The words with 'oo' are (", join('|',@doubleOs), ").\n";

# This loop also finds all words that match.
# In scalar context, the match returns true or false.
# The g (global) switch makes sure that each successive
# search begins where the last finished.
# The parens capture pieces of the match.  
# Inside the match, \1 \2 \3 are the values of the 1st, 2nd, 3rd captures;
# outside the match, $1, $2, $3 are those same saved pieces.
print "Looking for paired doubles...\n";
while (1){
  if ($string =~ m/\b(\w*(\w)\2(\w)\3\w*)\b/g){
    print " Found match on word '$1' that contains '$2$2$3$3' .\n";
  }
  else {
    print "Done.\n";
    exit;
  }
}
