This is the Hints and Answers page for the PERL/CGI midterm exam study guide. Check this page when you want to find out if any more answers or hints/discussion points have been posted. H1. What does the following statement do? $s = "\n Some UPPER case and SoMe LoWeR case LETTERS"; print lc $s; print "\L$s"; $s2 = lc $s; print $s2; $s3 = $s; $s3 =~ tr/A-Z/a-z/; print $s3; H2. Here is another hint/solution for a study guide question: $string = "Oh My! Oh boy!!! Yikes, this Perl is tough; yes."; $string =~ tr/!,;. //d; print "\n $string <---- has no spaces, !, commas, semicolons or " . "periods."; H3. Another hint and example of the power of binding operator and transliteration used to delete characters: $str = "Gosh, this is fun, fun, fun!!!"; $count = $str =~ tr/, !iu//d; print "$str <---- Originally had $count !, i, u, space " . "or comma characters."; ACTUAL OUTPUT FOLLOWS: -------------------------------------------------------------------------- Goshthssfnfnfn <---- Originally had 16 !, i, u, space or comma characters. -------------------------------------------------------------------------- H4. Another hint on power of transliteration operator and =~ binding. $str = "Gosh, this is fun, fun, fun!!!"; print "$str <---- Original string.\n"; $count = $str =~ tr/, !iu/\*/d; print "$str <---- $count !, i, u, space or comma converted to *??\n"; ------------------------------------------------------------------------ Gosh, this is fun, fun, fun!!! <---- Original string. Gosh*thssfn*fn*fn <---- 16 !, i, u, space or comma converted to *?? ------------------------------------------------------------------------ H5. Variation on Hint H4 example. Just to learn transliteration possibilities. print `cat hint5.pl`; $str = "Gosh, this is fun, fun, fun!!!"; print "$str <---- Original string.\n"; $count = $str =~ tr/, !iu/\*/; print "$str <---- $count * were added.\n\n"; ----------------------------------------------------- Gosh, this is fun, fun, fun!!! <---- Original string. ACTUAL Gosh**th*s**s*f*n**f*n**f*n*** <---- 16 * were added. OUTPUT ----------------------------------------------------- H6. Which one of the following opens a file f1.txt in your web folder, from a program in your web folder's cgi-bin folder? Remember, the web folder is the parent directory of the cgi-bin folder! open(FILEHANDLE, "../f1.txt"); or open(FILEHANDLE, "web/f1.txt"); H7. What does the cd .. statement do in Unix? It changes to the parent folder, and closes the child folder, which was the current directory. After the cd .. command, the parent folder is the current directory, and the previous folder is no longer current, i.e. it is a closed folder. H8. Hint (ANSWERS) for #32 in the study guide. sub ascending { $sorted = 1; # assume the @_ array is sorted, i.e. TRUE = 1. for ($i = 0; $i < (@_ - 1); $i++) { $sorted = 0 if $_[$i] > $_[ $i + 1 ]; } return $sorted; } English pseudocode of above: I. Assume list @_ is sorted into ascending order, by $sorted = TRUE; II. For each of the numbers except for the last number in the list @_, do the following: If it is BIGGER than the following number in the LIST, sorted is NOT TRUE. Set $sorted to FALSE; III. Return $sorted (the TRUE 1 or the FALSE 0) to whoever was wondering and wanting to know if the list was sorted. ANOTHER WAY TO DO THE II. STEP SHOWN ABOVE: for ($i = 0; $i < (@_ - 1); $i++) { if ( $_[$i] > $_[ $i + 1 ] ) { $sorted = 0; last; # <---- OPTIONAL... } } ANOTHER WAY TO DO THE ENTIRE PROGRAM: Make a 2nd array copy of data -------------------------------------------------------------------- sub ascending { $sorted = 1; # assume the @_ array is sorted, i.e. TRUE = 1. $arraySorted = sort @_; # make a sorted copy of the data for ($i = 0; $i < @_; $i++) { if ($_[$i] != $arraySorted[$i]) { sorted = 0; last; <------- $last is NOT needed } } return $sorted; } H9. perl -e ' $x = "Oh boy, \L THIS is FUN!\n\n"; $c = $x =~ tr/,! //; print " $x has $c special characters.\n\n"; ' What will be the output of the above 3 Perl statements run or -executed from the chaos command line using perl -e ' s1; s2; s3; ' ???? ----------------------- perl -e ' statements '