laziness, impatience, and hubris | |
PerlMonks |
Re: Generating all possible combinations from an AoAby LanX (Saint) |
on Apr 13, 2011 at 12:42 UTC ( [id://899174]=note: print w/replies, xml ) | Need Help?? |
Approaches:
1.MetaprogrammingGenerate dynamically a string of the above code with all nesting and eval it. (fast and intuitive!)
2. RecursionWrite a function X2() which gets two arrayrefs and returns an arr_ref of the cross product. Call it recursively until @array exceeded.
3. Reduceis a variation of the above, with List::Util::reduce
The function X() now works somehow like the X operator in perl6, but of course you could also use reduce { X2($a,$b) } @array directly. The function X2() could be rewritten with two nested maps, but thats a little two cryptic for my taste.
Bad ideas:
1. Globthat's a hack which only works for strings as element type, everything else will be stringified, eg refs!!!
Cheers Rolf UPDATES: * just noticed that you only want a simple concatenation of strings. That simplifies the code... * There is a problem with this code .. the first one to spot it gets upvoted! :)
In Section
Seekers of Perl Wisdom
|
|