Perl Monk, Perl Meditation  
PerlMonks 
Re^3: Finding all Combinations (subsets)by tye (Sage) 
on Oct 01, 2004 at 20:49 UTC ( #395756=note: print w/replies, xml )  Need Help?? 
I don't think this is really called "combinations" because the number of elements to select isn't specified beforehand. It is really finding all possible subsets, which is the same as finding the power set. Another approach to it uses Algorithm::Loops and is very simple; (well, if you understand nesting loops). You loop over 0..$#set finding the first element of the subset, then loop over the next element of the subset ($_+1..$#set), etc:
Then you can implement this same approach directly (without using the module):
And this code is so very simple, that I'm at a loss to explain why Limbic~Region's code is a little faster for large sets. His code goes about finding the subsets in a quite different order (and skips one subset) but the routines get called the same number of times and it appears to me that Limbic~Region's would do more work in an average call; but my benchmarks say that I'm wrong.  tye
In Section
Seekers of Perl Wisdom

