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

Hello, I have several arrays, kets say:
@a1 = qw(0 1 2 3 4); @a2 = qw(0 1 2 3 4 5 6 7 8); @a3 = qw(0 1); @a4 = qw(0 1 2 3 4 5);
I would like to generates simply (without for loops) the all permutation strings (5 x 9 x 2 x 6 options)
$str = $a1[$i] . " " . $a2[$j] . " ". $a3[$k]. " " . $a4[$l]
Any simple solution ? Thanks azaria

Replies are listed 'Best First'.
Re: array combinations
by moritz (Cardinal) on Jun 26, 2008 at 10:18 UTC
    Set::CrossProduct does exactly that. You have to build one loop to get the strings, though.

    You can also make up something with glob.

    Update: here's an example with glob:

    #!/usr/bin/perl use strict; use warnings; my @l = ( [1, 2, 3], [4, 5], [6, 7] ); my $glob = join '', map {'{'. join(',', @$_). '}'} @l; local $\ = "\n"; print for glob $glob; __END__ 146 147 156 157 246 247 256 257 346 347 356 357
Re: array combinations
by BrowserUk (Patriarch) on Jun 26, 2008 at 13:46 UTC
Re: array combinations
by dragonchild (Archbishop) on Jun 26, 2008 at 12:05 UTC
    permutations - CPAN is your friend.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: array combinations
by linuxer (Curate) on Jul 01, 2008 at 18:41 UTC

    What is "simple"?

    use strict; use warnings; my @a = ( 1,2,3 ); my @b = ( 4,5 ); my @c = ( 6,7 ); my ( $x, $y, $z ); my @total = map { $x = $_; map { $y = $_; map { join '', $x, $y, $_ } + @c } @b } @a; { local $, = local $\ = $/; print @total; } __END__

    Result:

    146 147 156 157 246 247 256 257 346 347 356 357