if (condition1) { statement1a; statement1b; } elsif (condition2) { statement2a; statement2b; } else { statement3a; statement3b; } nextStatement;
print "Give me a number?: "; my $number = <STDIN>; print "\n"; if ($number > 0) { print "$number is positive\n"; } elsif ($number < 0) { print "$number is negative\n"; } else { print "$number is not positive nor negative\n"; } exit;
if (conditon1) { statement1; } if (condition1) { statement1; } else { statement2; }
All of the above are ok.
In C, braces can be omitted if there is one statement (one-line conditional block).
if (a == 1) printf("a is 1\n");But in Perl,
if ($a == 1) { print ("$a is 1\n"); }
print ('$a is 1') if ($a == 1); print "I'm hungry.\n" if ($food == 0);
This reads more English-like.
$x
(e.g.,
-200, -0.9, 0, 0.0001, 1, 200, "abc"
, ""
), and think
when the following condition becomes true or false.
my $x = 1; if ($x) { print "$x is True\n"; } else { print "$x is False\n"; }
Comparison in Perl is a little more complicated.
== | equality |
!= | inequality |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
eq | equality |
ne | inequality |
lt | less than |
gt | greater than |
le | less than or equal |
ge | greater than or equal |
In string comparison, it is comparing the two expression alphabetically.
Try the following to clarify what is meant by comparing strings:
print "yes" if ("abc" ge "abd");
The following example should illustrate the difference.
my $a = 100; my $b = 99; if ($a > $b) { print "a is bigger\n"; } else { print "b is bigger\n"; } if ($a ge $b) { print "a is bigger alphabeticcally\n"; } else { print "b is bigger alphabetically\n"; }
Sometimes, you want to sort numerically. Other times, you want to sort alphabetically. Perl needs the two comparison mechanisms since Perl can flexibly convert integer, floating points, and strings.
&& | and |
|| | or |
! | not |
if ( $a > 0 && (! $b)) { ... }
unless (condition) { ... }
unless ($a > 0) { ... } if (! ($a > 0) { ... } if ($a <= 0) { ... }All of these 3 conditionals will do exactly same thing.
for ( initial; condition; increment) { statement1; statement2; }
for ($i = 0; $i < 10; $i++) { print "$i\n"; }
$i = 0;
)
$i < 10
)
print "$i\n";
).
$i++;
), and goes to step 2.
my @arrA = (); my $i; $max = 10; for ($i=0; $i < $max; $i++) { $arrA[$i] = $i * 3; } print "@arrA", "\n";
@seq
):
my @seq = qw( A T G C C C T T T );Using for loop, create an array,
@revseq
, which is the reverse of this seuqnce.
Then print this array without spaces (i.e., you should print out TTTCCCGTA).
#!/usr/bin/perl -w my @fuzz = ('dog','cat','ferret'); foreach my $item (@fuzz) { print "$item\n"; } exit;
foreach my $i (0..($max-1)) { ... }
foreach my $key (sort(keys %hashTbl)) { print "$key => $hashTbl{$key}\n"; }
>camel
ATGCCTATTGGA
>llama
ATGACTACCGGC
>owl
ATGAAGAGGGTC
#!/usr/bin/perl -w my @vote = ('y', 'n', 'n', 'y','n'); my %cnter; foreach my $yn (@vote) { $cnter{$yn} ++; } print "$cnter{'y'} Yes votes, $cnter{'n'} No votes\n"; exit;
while( condition ) { ... }
{...}
.
{...}
.
#!/usr/bin/perl my $cntr = 10; while ($cntr > 0) { print "In loop: "; print "counter is $cntr\n"; $cntr -= 3; } print "Outside of while loop: counter is $cntr\n"; exit;
for ($i = 2; $i < 20; $i++) { my $mod = $i % 7; print "$i % 7 = $mod\n"; }
!
) of the given integer.
A factorial of x is defined as x! = x * (x-1) * ... * 2 * 1.
#!/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.