[Israel.pm] Arrays

Peter Gordon peter at pg-consultants.com
Mon Aug 20 02:04:44 PDT 2007


Thanks. I like this one!

On Mon, 2007-08-20 at 10:03 +0300, Eitan Schuler wrote:
> 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.
> > >
> >
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl




More information about the Perl mailing list