You've been given good answers already, but here's how you could do it if you wanted to write the code rather than using a module or a built-in such as the glob function:
$ perl -e 'use strict; > use warnings; > > my @array = qw(A T C G); > my $length = 5; > my @result = @array; > > add_to_strings($length - 1); > print "@result"; > > > sub add_to_strings { > my $len = shift; > return if $len <= 0; > my @temp; > for my $string (@result) { > push @temp, $string . $_ for @array; > } > @result = @temp; > add_to_strings ($len - 1); > } > ' AAAAA AAAAT AAAAC AAAAG AAATA (... many string omitted for brevity ... +) GGGGA GGGGT GGGGC GGGGG
Here, the recursive call to add_to_strings is the key for generating arbitrarily nested loops.

You could do it without recursion, but it's likely to be a bit more complicated.

Also note that I have used here a global @result array for simplicity, but you could also pass @result in subroutine calls and returns to make it cleaner.

Update at 15:51 UTC: this is what the cleaner version might look like:

use strict; use warnings; my @array = qw(A T C G); my $length = 5; my @result = add_to_strings($length - 1, @array); print "@result"; sub add_to_strings { my $len = shift; my @temp_result = @_; return @temp_result if $len <= 0; my @temp; for my $string (@temp_result) { push @temp, $string . $_ for @array; } add_to_strings ($len - 1, @temp); }
You don't really need the @temp_result here, you could traverse directly @_, I just used @temp_result for better clarity, but this entails quite a bit of extra array copies, which might be inefficient for longer strings.

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.