| |
play lists and operators
Page history last edited by dmlond 2 yrs ago
- operators: create a perl application called aplus.pl and place the following code inside, save, exit, and run it.
my $a = 0; while ($a < 10) { print $a++."\n"; } exit;
Now change it to the following code, save, exit, and run it.
my $a = 0; while ($a < 10) { $a++; print $a."\n"; } exit;
- flow control: Change aplus.pl to use a post EXPR while test as follows:
my $a = 0; print $a++."\n" while ($a < 10); exit;
Some people find the former method easier to follow, while others like the latter method because it is more succinct. - lists: Here are a couple of different ways to print out the codons of a sequence. For now the sequence nucleoties are already present in an array. There is a function called split which we will talk about later which can create this array from a string.
In this case, the array is left unchanged, and an index is used to iterate through the array while the index is less than or equal to the length of the array. That index will also be used to test when to print the codon, when its modulus with 3 equals 0. my @seq = qw(a c g a t t a g c t a a c g t t); my $length = @seq; my $start = 0; my $codon = undef;
while ($start < $length) { Â Â if (defined($codon) Â Â && ( Â Â ($start % 3) == 0 Â Â )) { Â Â print &codonToAA($codon); Â Â $codon = undef; Â Â } Â Â $codon .= $seq[$start++]; } print "${codon}\n" if ($codon);
exit;
sub codonToAA { Â Â my $cod = shift; Â Â return "${cod}\n"; }
This one, again, leaves the array unchanged, but uses a foreach loop, and tests the length of codon directly. A bit more succinct.
my @seq = qw(a c g a t t a g c t a a c g t t); my $codon = undef;
foreach my $nc (@seq) { Â Â if (defined($codon) Â Â && ( length($codon) == 3 )) { Â Â print &codonToAA($codon); Â Â $codon = undef; Â Â } Â Â $codon .= $nc; } print "${codon}\n" if ($codon);
exit;
sub codonToAA { Â Â my $cod = shift; Â Â return "${cod}\n"; }
Finally, this one consumes the @seq array using shift, and the while loop ends when @array is empty (undefined, eg false).
my @seq = qw(a c g a t t a g c t a a c g t t); my $codon = undef;
while (@seq) { Â Â if (defined($codon) && (length($codon) == 3)) { print &codonToAA($codon); $codon = undef; } Â Â $codon .= shift @seq; } print "${codon}\n" if ($codon);
exit;
sub codonToAA { Â Â my $cod = shift; Â Â return "${cod}\n"; }
But before you go, I wanted to let you write your first perl program which takes commandline arguments. Call it printMyArgs.pl
#args are 'firstname' and then 'lastname' # note the use of shift on the implied @ARGV array my $fname = shift; my $lname = shift;
print "HELLO $fname $lname\n";exit;
Now, lets change it slightly to be a little more helpful with what arguments it expetct. Note, shift returns undef (false) when the @ARGV array is fully consumed. This allows you to test whether the assignment resulted in a true value, and die with a usage statement if it is not. This basically evaluates the return of shift on @ARGV, if it is true assigns it to the lvalue, if it is false, evaluates the die command which always dies, so there is nothing to return to the lvalue anyway.
my $usage = "usage: printMyArgs.pl firstname lastname\n"; # note the use of shift on the implied @ARGV array my $fname = shift or die $usage; my $lname = shift or die $usage;
print "HELLO $fname $lname\n";exit;
Try running this with no arguments: shell> perl printMyArgs.pl
With one argument: shell> perl printMyArgs.pl darin
And with two arguments:
shell> perl printMyArgs.pl darin london
play lists and operators
|
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.