CSCI 4230
Internet Applicaiton Development
Summer 2001
Suggested Solution to Homework #4

(1)    For example:

use strict;
use DBI;
#
#   populateImageTable.pl subdirectory image_desc_filename
#   This program populates the table ImageDescription(filename, pathname, description)
#   using the image description file image_desc_filename which stores an image
#   description one record per line in the format:
#
#   filename::description
#
# The image description file should be stored in the subdirectory.
#
# The pathname field of the table will be set to the command line argument
#   subdirectory.
#
# K. Yue June 29, 2001
#
#   Global variables for porting.
my $DSN = 'yuep';
my $DEBUG = 0;

@ARGV < 2 && die "usage: populateImageTable.pl subdirectory image_desc_filename\n";
my $subdirectory = $ARGV[0];
my $filename = $ARGV[1];
chdir($subdirectory) || die "Can't change to the subdirectory $subdirectory.\n";
open(IN,"<$filename")
 || die "Can't open image description file $filename.\n";

my $dbh = DBI->connect("dbi:ODBC:$DSN");
my %filenames = ();
my $currFile = 0;
my $filesInserted = 0;

print "File successfully open...\n";
#   Throw again the first line.
$_ = <IN>;
while ($_ = <IN>) {
   #   Process one record.
   chomp;
   my ($filename, $description) = split '::';
   $currFile++;

   #   Check file existence
   if (! -f $filename) {
      print "\n#$currFile: File $filename does not exist. Skipped.\n";
   next;
   }

   #   Check duplicate file names.
   if ($filenames{$filename}) {
      print "\n#$currFile: File name $filename already inserted to the table. Skipped.\n";
   next;
   }

   #   Insert the record.
   $filename =~ s/'/''/g;
   $description =~ s/'/''/g;
   my $sql = "INSERT INTO ImageDescription(filename,pathname,description) " .
             "values('$filename','$subdirectory','$description')";
   print "SQL => $sql\n" if $DEBUG;

   my $sth = $dbh->prepare($sql);
   $sth->execute();
   if ($sth->err()) {
      print "#$currFile: $filename insertion error.\n";
   print $sth->errstr() . "\n";
   }
   else {
      print ".";
      $filenames{$filename}++;
      $filesInserted++;
   }
}

$dbh->disconnect;

print "\nNumber of file names inserted: $filesInserted.\n";
exit 0;

(2)    For example,

<html>
<!--
       By:      Kwok-Bun Yue
       Date:    July 3, 2001
       Program: charCounts.html
-->

<head>
<title>Counting characters</title>
<script language="Javascript">
<!-- Hide scripts from older browsers.
//
//    Show the numbers of characters.
//
var LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

function countLetters() {
     var inputString = document.countForm.inputArea.value;
     var alpCounts = new Object();
  inputString = inputString.toLowerCase();
     regex = /[a-zA-Z]/gi;
     while (matchedArray = regex.exec(inputString)) {
      var ch = matchedArray[0];
   //  ch = ch.toLowerCase();
         if (alpCounts[ch]) {
            alpCounts[ch]++;
         }
         else {
            alpCounts[ch] = 1;
         }
     }

  for (var ch in LETTERS) {
         document.countForm[LETTERS[ch]].value =
    alpCounts[LETTERS[ch]] ? alpCounts[LETTERS[ch]] : "";
  }

  /*
  for (var ch in LETTERS) {
         document.countForm[LETTERS[ch]].value = "";
  }
  for (var ch in alpCounts) {
         document.countForm[ch].value = alpCounts[ch];
  }
  */
}

function resultHTML() {
 var result = "";
 result = "<table border=\"1\" width=\"80%\">"
 for (var i=0; i< LETTERS.length; i++) {
  if (i == 0) {
   result = result + "<tr>"
  }
  else if (i % 4 == 0) {
   result = result + "</tr><tr>";
  }
  result = result + "<td>" + LETTERS[i] + "&nbsp;&nbsp;<input type=\"text\" size=\"5\" name=\"" +
     LETTERS[i] + "\"></td>";
 }
 result = result + "</table>\n";
 return result;
}
 

// -->
</script>
</head>

<body bgColor='#CCCCFF'>

<h3 style="color:blue; size:+2">Counting Alphabets</h3>
Type in text in the input box and click the submit button
to see the numbers of letters.
<p>
<form name="countForm">
Input Text:
<br>
<textarea name="inputArea" cols="70" rows="5"></textarea>
<p>
<input type=button name="getCount" value="Count Letter" onClick="countLetters()">
<p>
Letter count result:
<p>
<script language="Javascript">
<!-- Hide scripts from older browsers.
document.write(resultHTML());
// -->
</script>
</form>
</body>
</html>