(1) For example,
use strict;
#
# Homework #1, Question 2, CSCI 4230, Fall
2001
# by K. Yue August 10, 2001
#
# See http://dcm.uhcl.edu/courses/csci4230/Fall2001/h1.html
for
# problem specification.
#
my $dictionary = "words.txt";
open (DICT, "<$dictionary") || die "Can't
open dictionary file $dictionary.\n";
# Get characters
@ARGV >= 1 || die "usage: h1q2.pl ch1 ch2
...\n";
my @ch = @ARGV;
foreach (@ch) {
die "Arguments must be single characters.\n"
if (length $_ > 1);
}
my $result = "";
my $resultCount = 0;
my $word = "";
WORD_LOOP:
while ($word = <DICT>) {
chomp $word;
foreach (@ch) {
next WORD_LOOP if $word !~ /$_/i;
}
# $word contains all characters.
$resultCount++;
$result .= "$resultCount: $word\n";
}
close DICT;
if ($resultCount) {
print "There are $resultCount words
containing all supplied characters:\n$result\n";
}
else {
print "There are no words containing
all supplied characters.\n";
}
exit 0;
(2) For example,
use strict;
use LWP::Simple;
@ARGV < 1 && die "usage: wordCount.pl
url";
my $url = $ARGV[0];
# Get all words from the url contents.
my $body = get($url) || die "Can't retrieve
the url $url\n";
my @words = $body =~ /[a-zA-Z]+/gs;
my %wordCounts = ();
foreach (@words) {
$wordCounts{lc($_)}++;
}
# print result.
print "Word counts for the url: $url\n\n";
foreach (sort keys %wordCounts) {
print "$_ => $wordCounts{$_}.\n";
}
exit 0;