PERL for Biologists

Course by Kurt Stüber

Previous, Part 4 ,Next

If-statement

The If-statement is a method to direct the flow of operations in a program in two directions, depending on a condition. Two alternatives are given and the program has to choose one of them.

$a = 5;
if( $a > 3 )
   {
   print "The value is bigger than 3.\n";
   }
else
   {
   print "The value is smaller or equal to 3.\n";
   }

$name = "Karl";
if( $name eq "Fred" )
   {
   print "The name is Fred.\n";
   }
else
   {
   print "The name is not Fred.\n";
   }

In the examples above either a numerical value is tested or a string is compared. In both cases the logical expression can be either true or false. If it is true then the first block is executed, if it is false the second block is executed. The second block can be omitted:

$a = 5;
if( $a > 3 )
   {
   print "The value is bigger than 3.\n";
   }

Then the program either executes the first block if the logical expression proves true or continues with the next program statement after the block. The block is then completely skipped and not executed.

String manipulation

String variables can be appended. The operator for this is the dot (.).

$no = "16";
$day = "Friday";
$month = "March";
$year = "2007";
$date = $day . $month . $no . $year;

This will result in the string $date equal to "FridayMarch162007". More readable is a version where small blanks are inserted in the string:

$date = $day . " " . $month . " " . $no . " " . $year;

This results in the string: "Friday March 16 2007"

Partial strings.

Using the build-in function substr you are able to cut partial strings from a longer string, for instance you can cut characters out of a word, words from a sentence, oligomers from sequence etc.. The basic syntax is: $partial_string = substr( $some_string, $offset, $length ); The $partial_string is cut from $some_string, beginning after $offset. The length of $partial_string is $length.

Example: Counting bases in DNA:

$seq = "atcgtgactgatgctgatcgatgatagtcatcga";
$no_of_a = 0;
$length_of_seq = length( $seq );
$position = 0;
while( $position < $length_of_seq )
   {
   if (substr( $seq, $position, 1 ) eq "a" )
      {
      $no_of_a++;
      }
   $position++;
   }
print "The sequence contains $no_of_a a's\n";

The build-in function length returns the number of characters in the string. $no_of_a++; is a shorthand version of $no_of_a = $no_of_a + 1; Build-in functions exists for numerous tasks. You can find lists of functions in every PERL textbook and at the webpage www.cpan.org. Or you may look at the list of functions used in this course.

Exercises:

Try to count all four bases in a sequence: $no_of_a, $no_of_c, $no_of_g and $no_of_t.

Try to count dimers and trimers using substr( $seq, $position, 2) and substr( $seq, $position, 3 ).

Solutions:


© 2001-2007, by Kurt Stüber.