Associative Arrays
#!/usr/local/bin/perl
#program that uses an associative array to count the number of capitalized words
$inputline = <STDIN>;
chop($inputline);
while ($inputline ne "") {
while ($inputline =~ /\b[A-Z]\S+/g) {
$word = $&; # $& contains the last pattern matched
$word =~ s/[;.,:-]$//; # remove punctuation
$wordlist{$word} += 1;
$inputline = ;
chop($inputline);
}
}
print ("Capitalized words and number of occurrences:\n");
foreach $capword (sort keys(%wordlist)) {
print ("$capword: $wordlist{$capword}\n");
}