-
Notifications
You must be signed in to change notification settings - Fork 13
/
lesson05_examples.pl
49 lines (39 loc) · 1.17 KB
/
lesson05_examples.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Cambridge Perl course
# Lesson 5 Examples
use strict;
use warnings;
my %geneticCode = ('TAT' => 'Tyrosine',
'TAC' => 'Tyrosine',
'CAA' => 'Glutamine',
'CAG' => 'Glutamine',
);
# Tedious hash lookup without loops
my @sequence = ('CAA','CAG','TAC');
print $geneticCode{$sequence[0]}, "\n";
print $geneticCode{$sequence[1]}, "\n";
print $geneticCode{$sequence[2]}, "\n";
# Foreach loop through the elements of an array
foreach my $codon (@sequence) {
print "$geneticCode{$codon}\n";
}
# Foreach loop through the keys of a hash
my %geneExpression = ('Beta-Catenin' => 2.5,
'Beta-Actin' => 1.7,
'Pax6' => 0,
'HoxA2' => -3.2,
);
foreach my $gene (keys %geneExpression) {
print "$gene: $geneExpression{$gene}\n";
}
# While loop over each key in a hash
while (my $gene = each %geneExpression) {
print "$gene: $geneExpression{$gene}\n";
}
# While loop with iterator variable $i
@sequence = ('CAA','CAG','TAG','CAT','GGT','GAG','GGC','CAG');
my $i = 0;
while ($i<5) {
print "$sequence[$i] ";
$i = $i + 1;
}
print "\n";