in reply to Help regarding the outout of the program

Two suggestions
  1. 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.
  2. 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."

Replies are listed 'Best First'.
Re^2: Help regarding the outout of the program
by roboticus (Chancellor) on Feb 24, 2010 at 12:04 UTC

    Utilitarian:

    ++ for removing the loop. I had a thought about your randombase routine, though. I came up with:

    sub randombase { # Prevent us from returning the same symbol my @list = grep { $_ ne $_[0] } @symbols; return $list[rand(@list)]; }

    ...roboticus