Two suggestions
- You do not need the for loop at all, you are merely counting to 30 and doing something once when you reach a magic number. This is particularly true if the starting dna sequence is greater than 30 bases.
- A look up table would be faster then an "until we get a different answer", this is particularly true if you are going to mutate your starting sequence repeatedly.
Try this for example
#!/usr/bin/perl
use strict;
use warnings;
print "The DNA sequence is : \n" ;
my $seq = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
print "$seq \n";
mutate ($seq);
exit;
sub mutate{
my($dna) = shift;
my $position = randposition($dna);
print " mutation at base position ". ($position+1) . "\n";
my $nucleotide = randombase(substr($dna,$position,1));
substr($dna, $position, 1, $nucleotide);
print "$dna\n";
}
sub randposition{
my($sequence) = @_;
return int rand length($sequence);
}
sub randombase{
my $base=shift;
my(%nucleo)=( 'A'=>['T','C','G'],
'T'=>['A','C','G'],
'C'=>['A','T','G'],
'G'=>['A','T','G'],
);
return $nucleo{$base}[rand(@{$nucleo{$base}})];
}
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.