in reply to More effective way of doing this

Other monks and myself have already provided some solutions.

But I think you might benefit from couple of comments on your code.

Any way to improve it?
Yes.

First, you should have this:

use strict;
near the top of your script and declare your variables with the my function.

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:

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++; }
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.

You should probably try to do it by yourself and avoid looking at the solution below:

use warnings; use strict; my @array = qw(A T C G); my $word_length = 4; my $max = ($#array+1)**$word_length ; my @count; $count[$_] = 0 for 1..$word_length; my $mainc = 0; for my $mainc (1..$max){ print $array[$count[$_]] for 1..$word_length; $count[1]++; for my $counter (1..$word_length) { if ($count[$counter] == $#array){ $count[$counter] = 0; $count[$counter + 1]++; } } print "\n"; $mainc++; }
Note: I really prefer the solution in my previous post because it is much easier to check the result (because it is in logical order). And I don't quite understand what your're doing in your program.

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.