Re: generating a wordlist from an array of arrays
- From: a.r.ferreira@xxxxxxxxx (Adriano Ferreira)
- Date: Fri, 30 Sep 2005 08:26:55 -0300
On 9/29/05, mark berger <mb@xxxxxxxxxxx> wrote:
> hey list. i stuck with gererating a wordlist from a changing
> multidimensional array. each entry in the array contains a list with the
> possible values.
I am bit rusty, because it took me a little too long to make it work,
but here is a recursive solution:
#!/usr/bin/perl
use strict;
use warnings;
# stuff( 'ab', qw(x y z) ) => qw(abx aby abz)
sub stuff {
my $first = shift @_;
return map { "$first$_" } @_;
}
# gen_list( [qw(a b)], [qw(x y)] ) => qw(ax ay bx by)
sub gen_list {
return () unless @_;
my $suffixes = pop @_;
return @$suffixes unless @_;
return map { stuff($_, @$suffixes) } gen_list(@_);
}
my @wordlayout = ([qw(a b)], [qw(c)], [qw(d e f)]);
my @list = gen_list(@wordlayout);
print "list: @list\n";
What surprised me was the hassle of my first solution which had too
many 'my' variables. But when I saw it working, a prune of the code,
preserving readability, made it shorter than I would have expected.
Regards,
Adriano.
.
- Follow-Ups:
- Re: generating a wordlist from an array of arrays
- From: John W. Krahn
- Re: generating a wordlist from an array of arrays
- References:
- generating a wordlist from an array of arrays
- From: Mark Berger
- generating a wordlist from an array of arrays
- Prev by Date: RE: readdir question
- Next by Date: Re: generating a wordlist from an array of arrays
- Previous by thread: Re: generating a wordlist from an array of arrays
- Next by thread: Re: generating a wordlist from an array of arrays
- Index(es):
Relevant Pages
|
|