Re: generating a wordlist from an array of arrays
good evening list. thanks for the replies and sorry that i couldn't find the time to reply sooner. sadly i'm still in a rush and haven't tried all of your solutions, hope i will get to it soon. for now i just go with one, so i can get the stuff i wanted to work. still curious about the other through, especially the recursive one. never quite get this kind of proposal the fast way. but i will check it out soon. Christer Ekholm schrieb: mark berger [EMAIL PROTECTED] writes: ... 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? ... i used your version, because for me it was the most straight forward approach, kind of where i wanted to go when i first thought about it. and no, it doesn't looked like mine at all. source scribble are gone but i tried to put the string together with indexes not concatenating and it was such mess. i tried to modify your example so it will also handle variable length pieces, and it seems to work too: @wordlayout = ( ['aa', 'A'], ['b', ''], ['c','', 'd', 'DD'], ); 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;'.\n; for my $idx ( 0 .. $#wordlayout ) { # Remove this levels char on our way out. $code .= 'my $len = length $char;'.\n; $code .= 'substr($str, \'-\'.$len, $len,);'.\n; $code .= }\n; } # curious? # print $code; # Now do it. eval $code; # The result is in @list. print join(\n,@list),\n; results: aabc aab aabd aabDD aac aa aad aaDD Abc Ab Abd AbDD Ac A Ad ADD thanks again to all of you, for taking the time to answer. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: generating a wordlist from an array of arrays
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
RE: generating a wordlist from an array of arrays
Mark, I like to use glob for such tasks. perldoc -f glob perldoc File::Glob bash-2.05b$ perl -le 'print for glob ({a,b}{c}{d,e,f})' acd ace acf bcd bce bcf Regards, Scott PS: I apologize for the top post. -Original Message- From: mark berger [mailto:[EMAIL PROTECTED] Sent: Thursday, September 29, 2005 1:27 PM To: beginners@perl.org Subject: generating a wordlist from an array of arrays 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). any hints on how to solve this? probably using recursion but i got no idea about it right now. and sorry for my bad english, hope i can make my problem understandable. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: generating a wordlist from an array of arrays
On Sep 29, mark berger said: 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. You want a cartesian cross product. And there's a module out there that does just that: Set::CrossProduct. http://search.cpan.org/~bdfoy/Set-CrossProduct-1.6/CrossProduct.pm Sample use: use Set::CrossProduct; my $iterator = Set::CrossProduct-new(['a','b'],['c'],['d','e','f']); my @wordlayout = $iterator-combinations; @wordlayout will be an array of array references (like ['a','c','f']). To turn them into strings, you could do: my @wordlayout = map join(, @$_), $iterator-combinations; any hints on how to solve this? probably using recursion but i got no idea about it right now. and sorry for my bad english, hope i can make my problem understandable. It would be a fun exercise to do this on your own, and yes, recursion is the most obvious way to do it. -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: generating a wordlist from an array of arrays
On Thu, Sep 29, 2005 at 07:26:51PM +0200 mark berger 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. 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 use references for multidimensional arrays: #!/usr/bin/perl use warnings; @w1 = ([a, b], [c], [d, e, f]); $str =; foreach $k (@{$w1[0]}) { foreach $l (@{$w1[2]}) { $str = [EMAIL PROTECTED] ; } } @w2 = split , $str; foreach (@w2) { print $_\n; } it's another solution ... hth -- GĂ©rard -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response