Class 37 review (08/01/2002)

  1. Review and study Wednesday's class. Excellent preparation for last assignment and for the final exam.

  2. Modular example should be VERY HELPFUL for your program project with sorted movies.

    Here is the running application: moviesSortByLengthVer3.cgi functions the same, but look at the much simpler, easier to understand PERL script code.

    1. How much easier is the following main program logic for you to read and understand and follow the logic of? The focus is on WHAT is happening in the major steps!
      ###########################################################################
      
      @myMovies = &readMoviesFileIntoArray("moviesPERL.txt");
      
      @myMovies = &sortByNameAndQuoteLength( @myMovies );
      
      $nestedOL = &makeNestedOLtags( @myMovies );
      
      print header;
      
      print start_html(-title   => "Movies SORTED by length - July 31st, 2002",
                       -BGCOLOR => "FFFF44"), 
      
            h2( "Movies sorted by Length and Nested OL lists\n[BR]\n" .
                "Version 3 - very modular, easier to understand and follow" ),
      
            $nestedOL, 
      
            end_html;
      
    2. Look how simple the sorting task is now. Only three PERL statements, and then the return statement to give the sorted array back to the main program. Prepare em, sort em, restore em, return em. Four statements!
      ######################################################################
      sub sortByNameAndQuoteLength
      {
         @movies = prepareRecordsForSorting( @_ );
      
         @movies = sort( @movies );
      
         @movies = &restoreRecordFormatsToOriginal( @movies );
      
         return @movies;
      }        
      
    3. Now look how clear and easy to follow the set of statement named prepareRecordsForSorting are:
      ######################################################################
      sub prepareRecordsForSorting
      {
         @moviesPrep = @_;
      
         for ($i = 0; $i < @moviesPrep; $i++)
         {
            $sortReadyRecord = &makeSortReadyRecord( $moviesPrep[$i] );
            $moviesPrep[$i] = $sortReadyRecord;
         }
         return @moviesPrep;        
      }
      
    4. Here is the procedure or subprogram that transforms the original moviesPERL.txt record into the new format needed, so it is prepared for sorting. I modified this so it just adds ONE new array entry here, but notice in include TWO TABS in that entry, to separate the 0003 from the Big from the 0057, for example, for the 3 character long Big film, with a 57 characters in length quote.
      ######################################################################
      sub makeSortReadyRecord     
      {
         $theRec = shift( @_ );
      
         @record = split (/\t/, $theRec);
      
         $quoteLen = &leadZeroLen( length $record[0] );
         $nameLen  = &leadZeroLen( length $record[1] );
      
         $movieName = $record[1];   
      
         $r = $nameLen . "\t" . $movieName . "\t" . $quoteLen;
         unshift(@record, $r);
      
         $theRec = join ("\t", @record);    
      
         return $theRec;
      }                  
      
    5. Finally, here is the sub to restore each of the 100 movie records back to their original format. This is a short little sub (6 inch Blimpies half sub instead of 12 inch full sub). Its very digestible code, only SIX LINES OF CODE. Easy to grasp! Easy to follow!
      ######################################################################
      sub restoreRecordFormatsToOriginal
      {
         foreach ( @_ )
         {
            @record = split (/\t/, $_);
      
            shift(@record);  shift(@record);  shift(@record);
      
            $rec = join ("\t", @record);
      
            $_ = $rec;
         }
         return @_;      # array now has records in their original format
      }                
      
  3. Mirroring movie names to create a liberty bell look, or a MOVIE MOUNTAIN of symmetrical sloping shaped words, in honor of ASCII art.