bisimen has asked for the wisdom of the Perl Monks concerning the following question:
I was trying to write a code that generates all possible combination of a word with X length and with any string of number/letters
This is what I came up with. So, this will work for any array of numbers or letters. But it will only do all possible combinations of them within a word length of 3. If say, I wanted a word length of 5, I would need to go an add more counts into the while loop, following the same pattern. I could do a while loop for a word length of 3, one for 4, one for 5... etc, but this will get messy and ugly in the end...
Any way to improve it? Or, is it hopeless and this is something I should be doing very differently? Thanks
use warnings; @array = qw(A T C G); $word_length = 3; $max = ($#array+1)**$word_length; $mainc = 0; $count1 = 0; $count2 = 0; $count3 = 0; while ($mainc != $max){ print $array[$count1]; print $array[$count2]; print $array[$count3]; $count1++; if ($count1 == $#array){ $count1 = 0; $count2++; } if ($count2 == $#array){ $count2 = 0; $count3++; } if ($count3 == $#array){ $count3 = 0; } print "\n"; $mainc++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More effective way of doing this
by holli (Abbot) on Oct 21, 2017 at 14:41 UTC | |
|
Re: More effective way of doing this
by haukex (Archbishop) on Oct 21, 2017 at 15:08 UTC | |
|
Re: More effective way of doing this
by Laurent_R (Canon) on Oct 21, 2017 at 15:43 UTC | |
|
Re: More effective way of doing this
by LanX (Saint) on Oct 21, 2017 at 15:03 UTC | |
|
Re: More effective way of doing this
by Laurent_R (Canon) on Oct 21, 2017 at 18:02 UTC | |
|
Re: More effective way of doing this
by Laurent_R (Canon) on Oct 21, 2017 at 18:51 UTC |