azheid has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks, please help. I want to generate all possible string combinations of N arrays. I am sure that there are several hundred ways that you guys could come up with to accomplish this. I only need one and it does not have to be efficient

A minimized version of my non-working solution is as follows:

my $string='echo {CAT,CAC}TGG{GTT,GTC,GTA,GTG}{CCT,CCC,CCA,CCG}'; my $combinations=`$string`; print $combinations;

The result that I want is

CATTGGGTTCCT CATTGGGTTCCC CATTGGGTTCCA CATTGGGTTCCG CATTGGGTCCCT CATTGGGTCCCC CATTGGGTCCCA CATTGGGTCCCG CATTGGGTACCT CATTGGGTACCC CATTGGGTACCA CATTGGGTACCG CATTGGGTGCCT CATTGGGTGCCC CATTGGGTGCCA CATTGGGTGCCG CACTGGGTTCCT CACTGGGTTCCC CACTGGGTTCCA CACTGGGTTCCG CACTGGGTCCCT CACTGGGTCCCC CACTGGGTCCCA CACTGGGTCCCG CACTGGGTACCT CACTGGGTACCC CACTGGGTACCA CACTGGGTACCG CACTGGGTGCCT CACTGGGTGCCC CACTGGGTGCCA CACTGGGTGCCG

The code returns {CAT,CAC}TGG{GTT,GTC,GTA,GTG}{CCT,CCC,CCA,CCG}

Replies are listed 'Best First'.
Re: System Command using Bash Expansion
by kcott (Archbishop) on Mar 30, 2014 at 03:43 UTC

    G'day azheid,

    You can just use the glob function: there's no need to use the shell.

    #!/usr/bin/env perl -l use strict; use warnings; my @x = glob '{CAT,CAC}TGG{GTT,GTC,GTA,GTG}{CCT,CCC,CCA,CCG}'; print "@x";

    Output:

    CATTGGGTTCCT CATTGGGTTCCC CATTGGGTTCCA CATTGGGTTCCG CATTGGGTCCCT CATTG +GGTCCCC CATTGGGTCCCA CATTGGGTCCCG CATTGGGTACCT CATTGGGTACCC CATTGGGTA +CCA CATTGGGTACCG CATTGGGTGCCT CATTGGGTGCCC CATTGGGTGCCA CATTGGGTGCCG +CACTGGGTTCCT CACTGGGTTCCC CACTGGGTTCCA CACTGGGTTCCG CACTGGGTCCCT CACT +GGGTCCCC CACTGGGTCCCA CACTGGGTCCCG CACTGGGTACCT CACTGGGTACCC CACTGGGT +ACCA CACTGGGTACCG CACTGGGTGCCT CACTGGGTGCCC CACTGGGTGCCA CACTGGGTGCCG

    Be aware that there's a system-dependent limit on the size of the result. See GLOB_LIMIT, in File::Glob, for details.

    -- Ken

Re: System Command using Bash Expansion
by eye (Chaplain) on Mar 30, 2014 at 03:22 UTC
    I like a Perl solution:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @result = ( '' ); my @plan = ( [ qw/ CAT CAC / ], [ qw/ TGG / ], [ qw/ GTT GTC GTA GTG / ], [ qw/ CCT CCC CCA CCG / ], ); for my $step ( @plan ) { my @new_result; for my $suffix ( @$step ) { push @new_result, map { "$_$suffix" } @result; } @result = @new_result; } print Dumper(\@result);
    though I was surprised that bash's (4.1.2) echo would do what you want:
    #!/bin/bash echo {CAT,CAC}TGG{GTT,GTC,GTA,GTG}{CCT,CCC,CCA,CCG}
Re: System Command using Bash Expansion
by karlgoethebier (Abbot) on Mar 30, 2014 at 13:15 UTC
    ...all possible string combinations of N arrays...there are several hundred ways...

    Why don't you take a look at Math::Combinatorics and Algorithm::Permute?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: System Command using Bash Expansion
by Anonymous Monk on Mar 30, 2014 at 02:34 UTC

    I am sure that there are several hundred ways that you guys could come up with to accomplish this. I only need one and it does not have to be efficient

    You can come up with it also, all you have to do is search combinations, combinations, combinations

Re: System Command using Bash Expansion
by Anonymous Monk on Mar 30, 2014 at 18:58 UTC

    A regex-based solution:

    my $string='{CAT,CAC}TGG{GTT,GTC,GTA,GTG}{CCT,CCC,CCA,CCG}'; 1 while $string =~ s/([^{\s]*){(.*?)}(\S*)/@{[map $1.$_.$3, split ',', + $2]}/; print $string;