Hi, Really thanks for your answer, yes the second one doesnt compile because of some error typos like 'if ($elements=>{$elm})', without the hash variable ($elements) it works, But also, you are right, your code is faster than mine, :D Again thanks for your answer.
Regards, On Wed, Aug 13, 2008 at 1:52 AM, Rob Dixon <[EMAIL PROTECTED]> wrote: > Pablo Zea Aranibar wrote: > > > > Hi, realy i need your help with this algorithm. > > what I need is another algorithm in order to improve my own. > > > > example: > > > > my $firsSet = [ 'a', 'b', 'c', 'd', 'e' ]; > > my $secondSet = [ 'x', 'y', 'z' ]; > > my $thirdSet = [ 'a', 'x', 'b', 'y' ]; > > my $fourthSet = [ 'b', 'd', 'z' ]; > > my $counter = 0; > > > > foreach my $elm1 (@{$firstSet}) > > { > > foreach my $elm2 (@{$secondSet}) > > { > > foreach my $elm3 (@{$thirdSet}) > > { > > foreach my $elm4 (@{$fourthSet}) > > { > > print "$counter : $elm1, elm2 , elm3 , elm4\n"; > > $counter ++; > > } > > } > > } > > } > > # It prints each possible combination taken one element for each set. > > > > > > another (better way): > > > > my @Set = (); > > my $elements = {a=>1, b=>1, c=>1, d=>1, e=>1, x=>1, y=>1, z=>1}; > > $Set[0] = [ 'a', 'b', 'c', 'd', 'e' ]; > > $Set[1] = [ 'x', 'y', 'z' ]; > > $Set[2] = [ 'a', 'x', 'b', 'y' ]; > > $Set[3] = [ 'b', 'd', 'z' ]; > > my $counter = 0; > > > > @result = @{$Set[0]}; > > for (1 .. (scalar @Set) - 1) { > > my $temp = []; > > foreach my $result (@result) { > > foreach my $elm (@{$Set[$_]}) { > > if ($elements=>{$elm}) { #if I dont want repetitions > > push @{$temp}, $result.', '.$elm; > > $elements=>{$elm}=0; > > $counter++; > > }; > > }; > > }; > > @result = @{$temp}; > > }; > > print "@result\n"; > > > > > > But, both are still too slow. > > So, can you give me an idea to improve it?. > > someone tell me about mask, and bit operations. but i really dont know > how > > to do it. so.. any suggestion?. > > there was a thread in a forum > > (here< > http://forums.devshed.com/software-design-43/algorithm-design-536480.html > >), > > but the solution was for python (recursive using "yield"). > > Looking at your second example (which doesn't compile, so it's not > surprising > it's too slow!) I think you want all combinations of the sets that don't > have > repeating letters. The program below will do that, and if you want all > combinations regardless of repeated characters simply remove the line > > s/$c//g foreach @sets; > > Finally, if you prefer a comma-separated form of output, just change the > print > line to read > > print join(', ', split ''), "\n" foreach @results; > > I hope this helps. I had fun writing it! > > Rob > > > use strict; > use warnings; > > my @sets = qw( > abcde > xyz > axby > bdz > ); > > my @results = combine(@sets); > print "$_\n" foreach @results; > > sub combine { > > return () if grep length == 0, @_; > > my @initials = split '', shift; > return @initials unless @_; > > my @return; > foreach my $c (@initials) { > my @sets = @_; > s/$c//g foreach @sets; > push @return, map "$c$_", combine(@sets); > } > > @return; > } > > >