[Israel.pm] Arrays

Eitan Schuler eitan.schuler at gmail.com
Mon Aug 20 00:03:50 PDT 2007


Once Ran Eilam has showed me the following:

print Dumper combs(
	[qw(a b)],
	[qw(foo bar baz)],
	[qw(one two)]
);

sub combs {
    return ([]) if !scalar(@_);
    my $atoms = shift;
    map
        { $tuple = $_; map [$_, @{$tuple}], @{$atoms} }
            combs(@_);
}

------------------------
Exlanation:
Assume we have combs(N-1 lists) working, just need to compute combs(N).
combs(N) = combs(N-1) X [the 1st list]
Where X is cartesian multiplication- the inner map above.

--Eitan



On 8/20/07, Peter Gordon <peter at pg-consultants.com> wrote:
> On Mon, 2007-08-20 at 09:32 +0300, Gaal Yahas wrote:
> > On 8/20/07, Peter Gordon <peter at pg-consultants.com> wrote:
> > > I have a number of arrays
> > [...]
> > > and I want to find all possible combinations of all elements
> > [...]
> > > Is there a package which iterates over arrays?
> >
> > You mean you don't know in advance how many arrays you have (or just
> > want to factor out the nestedness in your code)?
>
> The number of arrays varies with each iteration
>
>
>
> >  Take a look at
> > Algorithm::Loops, specifically at the "Nested loops" stuff.
> >
>



More information about the Perl mailing list