Homework 2.a

#!/usr/bin/perl
#========================================================================================
# PROGRAMMER: Prabesh Vijayananda
# CLASS.SEC : CSCI 4230.02
# INSTRUCTOR: Dr. Kwok-Bun Yue
# FILE NAME : h2q1.pl
# ASSIGN. # : 2
# DUE DATE : Sep 16, 2001
# URL : http://nas.cl.uh.edu/vijayanandap/
#----------------------------------------------------------------------------------------
# DESCRIPTION :
#
# http://nas.cl.uh.edu/vijayanandap/pages/hw/

# INPUT :

# OUTPUT :
#========================================================================================
#course: csci 4230.02


#take argument from the user and check it
@ARGV < 1 && die " Usage: h1q2.pl ch1 ch2... \n";

#get the argument length
$arglen = @ARGV;

#open input file
$FILENAME = "words1.txt";
open(IN, $FILENAME) or die "Can't open input file $FILENAME.\n";

$i=0; #initialize i to 0
$numword=0;
#print statement global
print "There are $numword words containing all supplied characters: \n" ;

#while loop
while ($word = <IN>)
{
chomp;

foreach $ARGV(@ARGV)
{
if($word =~ /$ARGV/i)
{
$count++;
}
else{
$count = 0;

}



if( $count == $arglen){

$i++;

print " $i : $word ";
}
else{
# print "There are no words containing all supplied characters.\n";
# last;
}
}

}
$numword++;
exit 0;

 

Homework 2.b

#!/usr/bin/perl
#========================================================================================
# PROGRAMMER: Prabesh Vijayananda
# CLASS.SEC : CSCI 4230.02
# INSTRUCTOR: Dr. Kwok-Bun Yue
# FILE NAME : h2q2.pl
# ASSIGN. # : 2
# DUE DATE : Sep 16, 2001
# URL : http://nas.cl.uh.edu/vijayanandap/
#----------------------------------------------------------------------------------------
# DESCRIPTION :
#
# http://nas.cl.uh.edu/vijayanandap/pages/hw/

# INPUT :

# OUTPUT :
#========================================================================================
#course: csci 4230.02

#h2a2.pl


#use strict;
use LWP::Simple;
use LWP::UserAgent;
use URI;
@ARGV < 1 && die "Usage: h2p2.pl\n";

#To get the URL
my $geturl = $ARGV[0];

my $words =" "; # contents of the web page
$webcontents = get($geturl) or die " URL not found.\n";
# print $webcontents;

$webcontents=~ s/^\s*//;
$word = $webcontents[0];

%wordCounts = ();

#$words[0] =$webcontents;
print $webcontents;

#this piece of code is taken from DR.yue's homepage
# Loop through the file.
while ($_ = <$webcontents>) {
# For every line.
chomp;
print $webcontents;
# Extract words from the line.
# Remove the leading white spaces.
# s/^\s+//;
@words = split /\s+/, $_;
# For each word:
foreach $word (@words) {
# Convert the word to upper case.
$word = uc($word);
# Use the word as the key to update
# %wordCounts.
$wordCounts{$word}++ if $word;

}
}

# Close input file.
close(IN);

# Output word counts: %wordCounts
foreach $key (sort keys %wordCounts) {
print "$key => $wordCounts{$key}.\n";
}
exit 0;