Difference between revisions of "Perl example code"
From irefindex
(Created page with "Excercise 12 <pre> #!/usr/bin/perl use strict; use warnings; #TASK: Write a program that has one function. #Use a variable named “$some_variable” in this #function and in t…") |
|||
Line 76: | Line 76: | ||
exit(); | exit(); | ||
</pre> | </pre> | ||
+ | |||
+ | excercise13input.txt | ||
+ | <pre> | ||
+ | #this is the regex that we are looking for in the lines below | ||
+ | #/^ATG?C*[ATCG]+?A{3,10}$/ | ||
+ | |||
− | + | ATGCCCAA | |
+ | ATGCCCAAAA | ||
+ | ATGCCCAAAAAAAAAAAAAAAAA | ||
+ | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
+ | DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD | ||
</pre> | </pre> | ||
Revision as of 21:10, 26 May 2011
Excercise 12
#!/usr/bin/perl use strict; use warnings; #TASK: Write a program that has one function. #Use a variable named “$some_variable” in this #function and in the main body of the program. #Prove that you can alter the value of #$some_variable in the function without #changing the value of $some_variable in the #the main body of the program #declare variables used in the main routine my $some_variable; my $some_variable_changed_by_subroutine; #main routine $some_variable = 10; print "i am in the main routine and some_variable is: $some_variable\n"; $some_variable_changed_by_subroutine = subroutine($some_variable); print "i am in the main routine some_variable is now: $some_variable\n"; print "i am in the main routine some_variable_changed_by_subroutine is: $some_variable_changed_by_subroutine\n"; #a subroutine that uses a variable with the same name as a variable in the main routine sub subroutine{ my $some_variable; $some_variable = $_[0]; print "i am in the subroutine and some_variable is: $some_variable\n"; ++$some_variable; print "i am in the subroutine and some_variable is now: $some_variable\n"; return $some_variable }
Excercise 13
#!/usr/bin/perl use strict; use warnings; #TASK: Read lines from input file – #print lines that match a regular expression #open input and output files open(IN,"excercise13input.txt"); open(OUT,">excercise13output.txt"); #read the input file line-by-line #for each line ask if it matches the regex #print it in the output file while(<IN>){ chomp; if ($_ =~ /^ATG?C*[ATCG]+?A{3,10}$/) { print OUT "$_\n"; print "$_\n"; } } #close input and output files close(IN); close(OUT); exit();
excercise13input.txt
#this is the regex that we are looking for in the lines below #/^ATG?C*[ATCG]+?A{3,10}$/ ATGCCCAA ATGCCCAAAA ATGCCCAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD