in reply to More effective way of doing this
But I think you might benefit from couple of comments on your code.
Any way to improve it?Yes.
First, you should have this:
near the top of your script and declare your variables with the my function.use strict;
Second, you should try to properly indent your code. The computer doesn't care, but human readers do.
Third, using variables named count1, count2 and so on is usually a red flag. You should probably use an array of values. This is not sufficient to make your algorithm able to handle word lengths other than 3, but it is certainly a precondition to make this possible (within the context of your code).
So, as a first step, your code could be rewritten as follows:
Now, if you look at the three if statements, you might be able to use a counter (in a loop) as an index for the @count array instead of hard coding the indices as I have done above, thereby making it possible to manage strings of arbitrary lengths.use warnings; use strict; my @array = qw(A T C G); my $word_length = 3; my $max = ($#array+1)**$word_length; my @count; $count[$_] = 0 for 1..$word_length; my $mainc = 0; while ($mainc != $max){ print $array[$count[$_]] for 1..$word_length; $count[1]++; if ($count[1] == $#array){ $count[1] = 0; $count[2]++; } if ($count[2] == $#array){ $count[2] = 0; $count[3]++; } if ($count[3] == $#array){ $count[3] = 0; } print "\n"; $mainc++; }
You should probably try to do it by yourself and avoid looking at the solution below:
Update at 18:53 UTC: I just noticed that the result is not correct, because the algorithm of your original program is not correct (see this post: Re: More effective way of doing this below). I won't attempt to fix it, though, because, as noted earlier, I don't really understand how your original program is supposed to work.
|
|---|