Perl - Application of Regular Expressions
In a Nutshell - CIW Course Section 2, Part B2, Chapter 3
Regular Expressions and Hashes
While regular expression do not do anything special or unique with hashes. The use of hashes with regular expressions can be made to perform useful functions.
The course uses an example to demonstrate a word count routine which I found a little difficult to follow, so I have amended it slightly to better illustrate it's operation:
$myStr = "the brown fox accompanied the red fox";
while ($myStr =~ s/(\w+)(.*)/$2/)
{
$word = $1;
$wordHash{$word}++;
print("$myStr\n");
}
while (($word, $count) = each(%wordHash))
{
$wordArray[$i] = "$word\t$count";
print("$wordArray[$i]\n");
$i++;
}
The first while loop is where all the counting is done, and this utilises the $myStr =~ s/(\w+)(.*)/$2/ substitution expression. '(\w+)' is looking for one or more alphanumeric characters - the word. The parentheses denote it should be remembered, and as it is the first parenthesised block, it will be assigned to $1. '(.*)' is looking for zero or more occurrences of any character - the remaining string. This is assigned to $2 and is used as the replacement text. This has the effect of isolating the word and removing it from the string. The print("myStr\n") line has been added to the loop to show this.
The scalar variable '$word' is assigned the remembered value from $1, which will be the word that was found. This is used as the 'key' for the hash 'wordHash' and the corresponding 'value' will be used for the word count. The line $wordHash{$word}++; will look up, or create the entry in the hash for the supplied word and increment the count value.
The second loop steps through each pair in the hash and assigns the word and it's count, separated by a tab symbol, to an array, which is then printed.
Hopefully, this now makes sense!
RPN Solving
RPN or Reverse Polish Notation is a way of expressing algebraic expressions.
For example the expression: "(4 + 9) * (7 - 2)" can be shown, using RPN as: "4 9 + 7 2 - *".
The course includes a fairly detailed example of how RPN can be parsed and evaluated using Perl regular expressions. I don't intend to cover it here, as much of it has been covered before.

