String Processing

Conditionals

In this lesson the student will learn how to:
  1. Use if-elsif-else constructs
  2. Use unless construct
  3. Use if after statement
  4. Use or construct
By the end of this lesson the student will be able to:

	Write a simple quiz script for the amino acid
	names and symbols.

Amino Acid Symbols

Here are the amino acid symbols:


	@symbols = ('S', 'F', 'L', 'Y', 'C', 'W', 
	            'P', 'H', 'Q', 'R', 'I', 'M',
                    'T', 'N', 'K', 'V', 'A', 'D', 
                    'E', 'G' );


Here are the corresponding names:

	@names = ("serine", "phenylalanine", "leucine", "tyrosine", "cysteine",
                  "tryptophan", "proline", "histidine", "glutamine", "arginine",
                  "isoleucine", "methionine", "threonine", "asparagine", "lysine",
                  "valine", "alanine", "aspartic acid", "glutamic acid", "glycine");

You will notice that most of the symbols and names start with the same letter. However, since, for instance, there are three amino acid names which begin with t (tyrosine, tryptophan, threonine), it is necessary to use other letters. Why tryptophan got W as it's symbol is not immediately obvious. The letters Q for glutamine and K for lysine also seem sort of arbitrary, but if you inspect the symbols and names carefully you may discover how the symbols were paired with the names.

There are other amino acids other than these which are coded for by the genetic code. For instance, you may have heard of creatine, carnitine or taurine which are sometimes used as dietary supplements. Most of the 20 amino acids used in the genetic code are also available as dietary supplements. One notable exception is tryptophan which was available as a dietary supplement until a food poisoning problem occured in the late 1980's.

#!/usr/bin/perl @things = ( 5, "CA", 1, 10, "thing", 2, 7); foreach $x (@things){ print "$x: "; if($x =~ /\D+/){ print "not a number\n"; } elsif($x>5) { print "number greater than five\n"; } elsif($x==5){ print "number equal to five\n"; } elsif($x<5){ print "number less than five\n"; } else{ print "unknown thing\n"; } }
The following program shows examples of how to use the unless, if, and or constructs.
#!/usr/bin/perl @a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); print "UNLESS:\n"; foreach $item (@a){ print "$item is EVEN\n" unless ($item%2==1); } print "IF:\n"; foreach $item (@a){ print "$item is EVEN\n" if ($item%2==0); } sub check_item{ ($i) = @_; if($i%2==0){ return 0; } else{ return 1; } } print "OR:\n"; foreach $item (@a){ check_item($item) or print "$item is EVEN\n"; }
Inspect the or example carefully. Notice that the check_item subroutine returns either 0 or 1.

ASSIGNMENT:

Modify the following code according to the specifications which follow:

#!/usr/bin/perl @symbols = ('S', 'F', 'L', 'Y', 'C', 'W', 'P', 'H', 'Q', 'R', 'I', 'M', 'T', 'N', 'K', 'V', 'A', 'D', 'E', 'G' ); @names = ("serine", "phenylalanine", "leucine", "tyrosine", "cysteine", "tryptophan", "proline", "histidine", "glutamine", "arginine", "isoleucine", "methionine", "threonine", "asparagine", "lysine", "valine", "alanine", "aspartic acid", "glutamic acid", "glycine"); $cnt = 0; $right = 0; foreach $item (@symbols){ print "Symbol: $item\n"; print "Name: "; $user = <STDIN>; chomp($user); if($user eq $names[$cnt]){ print "correct\n"; $right++; } else{ print "wrong\n"; } $cnt++; } print "RESULTS: $right correct out of 20\n";