next up previous
Next: File I/O Up: perl1st Previous: Quick elements of perl

Subsections

Flow control

Conditional and looping constructs are similar to C, but there are some differences in details.

if

Logical operator

Comparison in Perl is a little more complicated.

Boolean logic

unless

for

Exactly like C:

foreach

while

Same as in C:

Homework:

  1. Make a program which will keep asking an integer as the input forever (use an infinite loop), and then calculate and print the factorial (!) of the given integer.

    A factorial of x is defined as x! = x * (x-1) * ... * 2 * 1.

  2. Starting from the following code, make a program to count the base frequency:
    #!/usr/bin/perl -w
    my $seqString = "AGGTGACCTTAGGCTTATAGCTATTA";
    
    ### create an array: each element is a single base.
    my @bases = split(//, $seqString);
    

    The split command creates an array by splitting the character string into each character. In other words, the array @bases become ('A', 'G', 'G', 'T', ....).

    Count how many 'G' or 'C' this sequence contains (GC content), and print out the results.

    Hint 1: You want to loop through all elements of the array, and check whether each nucleotide is equal to 'G' or 'C' (remember '||' means OR?). If it matches, you want to increment the counter. After you are done looping through all elements, you can print out the value of the counter.

    Hint 2: There are several way of looping through all elements of an array.

    foreach $nucleotide (@bases) { 
       ... 
    }
    
    might be the easiest. The variable $nucleotide become 'A' at first, then it become 'G' in the 2nd loop.

  3. Can you modify this program to print out base composition? You want to print out the percent of A, T, G, and C. You have to count occurences of A, T, G, and C. Then divide the occurence by the total sequence length.


next up previous
Next: File I/O Up: perl1st Previous: Quick elements of perl
Naoki Takebayashi 2011-10-06