Perl 2

Control Structures: What is Truth?

James Tisdall

Genome Informatics

Suggested Reading

Chapter 4 of Learning Perl.

Lecture Notes

Problems

  1. Modify the "add" script from yesterday so that it checks that both arguments are defined. This will allow negative numbers and 0:
          % add 2
          Please provide two arguments.
    
          % add 2 -5
          -3
          

  2. Modify the script again so that it checks that both arguments are positive integers. Zero is allowed, but -1 is not:
          % add 2 -5
          Please provide two positive integers.
          

  3. Rewrite the script to use -w and use strict.

  4. Write a script to read a line of DNA from the keyboard and print "yes!" if it contains an EcoRI site (GAATTC or gaattc).

  5. Write a script to compare two strings given on the command line arguments and print "right order" if they are in alphabetic order, and "wrong order" if they are not:
          % order Fred Lucy
          right order
    
          % order Lucy Fred
          wrong order
          

  6. Write a script to compare two strings given on the command line and print them out in correct alphabetic order:
          % reorder Fred Lucy
          Fred Lucy
    
          % reorder Lucy Fred
          Fred Lucy
          
  7. Write a script named "same.pl" to read two strings from the terminal. Compare them in a case-sensitive manner and print "same" if they are the same, "different" if they are different:
          % same.pl
          Enter string 1: lucy
          Enter string 2: Lucy
          different
          
  8. Modify this script to compare the strings in a case-INsensitive manner (hint, use the "lc" or "uc" functions change to upper or lowercase.
  9. Modify this script to use the \U and/or \L string escapes.
  10. Write a script named "percent" to calculate percentages, where the percentage is $a/($a+$b) * 100. Make sure that the script does not crash when given two numbers that add up to zero:
          % percent.pl 50 150
          25%
          
          % percent.pl 50 -50
          You are trying to trick me! at line 4.
          
  11. Modify this script to use the printf() function to produce nicely formatted floating point numbers (hint: after checking the Perl books, try "man sprintf" and "man printf" to learn about this wonderful function).
          % percent.pl 50 150
          25.00 %
          
  12. Write a program to check the command-line argument for a valid american social security number (999-99-9999). Print "yes" if it matches:
          % ssno.pl 058-28-1282
          yes
    
          % ssno.pl "bananas are yummy"
          no
          
  13. Modify the previous program to read the social security number from the keyboard.
  14. Write a program named "pali.pl" to detect palindromes. It must be able to handle changes in case.
          % pali.pl "Madam in Eden Im Adam"
          yes!
    
          % pali.pl ggaattcc
          yes!
    
          % pali.pl "cold spring harbor laboratory"
          no!
          
  15. Modify the program to work even if there is extraneous punctuation:
          % pali.pl "A man, a plan, a canal... Panama!
          yes!
          
    (Hint: Look up the s/// pattern matching & substitution function in the Perl reference guide. We will cover this formally in a few days.)


Genome Informatics
Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Wed Oct 20 10:44:58 EDT 2004