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:

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.


In reply to Re: More effective way of doing this by Laurent_R
in thread More effective way of doing this by bisimen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.