mark berger <[EMAIL PROTECTED]> writes:

> 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.
>
> fe:
>
> @wordlayout = ((a, b),                # possible values for 1st char
>            (c),               # possible values for 2nd char
>            (d, e, f));                # possible values for 3rd char
>
> the following wordlist should be generated:
>
> acd
> ace
> acf
> bcd
> bce
> bcf
>
> the only thing i came up with so far, is to generate some for loops
> based on the array structure (how many chars, with how many posible
> values) on fly and pass this to eval. pretty ugly (at least the way i
> thought it out).

I gave that some thought. And I think it actually might be a good
solution. I wonder which method is more efficient, dynamic
code-generation or recursion?

Here is my try at it, does it look like yours?

@wordlayout = (['a', 'b'],
               ['c'],
               ['d','e','f'],
    );

for my $idx ( 0 .. $#wordlayout ) {
    # A loop for each level.
    $code .= 'for my $char ( @{$wordlayout[' . $idx . ']} ) {'. "\n";
    # concat chars
    $code .= '$str .= $char;'."\n";
}
# At the innermost level, extract a word.
$code .= 'push @list,$str;';

for my $idx ( 0 .. $#wordlayout ) {
    # Remove this levels char on our way out.
    $code .= 'substr($str,-1,1,"");';
    $code .= "}\n";
}

# Now do it.
eval $code;

# The result is in @list.
print join("\n",@list),"\n";



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to