Simple Subset Question 
Author Message
 Simple Subset Question

I'm trying to write an algorithm that gives me all combinations of an array

ab ac ad bc bd cd
    or
 aa ab ac ad bb bc bd cc cd

I've tried variations on the example program below to no avail. It seems
that I should be able to delete the first item of the array each time
through the loop and end up with all the combinations. However, if I comment

For example:
aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd
They are there, but there's too much redundancy for what I want.

It looks like a problem for CS101, but I'm at a loss to figure out what the
problem is.

#!/usr/local/bin/perl -w



        print "$item times $element\n";

Quote:
}


Quote:
}

---------------------------
Bob Gwynne

Speech Comm
University of Tennessee, Knoxville

JAPN (Just Another Perl Newbie)



Mon, 03 Sep 2001 03:00:00 GMT  
 Simple Subset Question

Quote:

> I've tried variations on the example program below to no avail. It seems
> that I should be able to delete the first item of the array each time
> through the loop and end up with all the combinations. However, if I

> combinations.  For example: aa ab ac ad ba bb bc bd ca cb cc cd da db dc
> dd They are there, but there's too much redundancy for what I want.
> #!/usr/local/bin/perl -w



>         print "$item times $element\n";
>     }

> }

In man perlsyn, it says:

     If any part of LIST is an array, foreach will get very confused if you
     add or remove elements within the loop body, for example with splice.
     So don't do that.

which is probably part of the problem.  I think this is one of the few
times that you want to use an iterator in Perl, or at least that's the
first solution that comes to mind:




    print "$array[$i]$array[$j] ";
  }

Quote:
}

print "\n";
aa ab ac ad bb bc bd cc cd dd

--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker





Wed, 05 Sep 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Perl tuning/speed question: all subsets of a set of length k -- keeping the inital order

2. Subset Question

3. XS - Simple answer to simple question?

4. Simple Question from a simple chap!

5. Simple Question - LWP::Simple not accessing the internet

6. simple, simple question

7. Simple Unix Question, ( Simple for you maybe )

8. Simple simple question

9. Small subset of Perl....

10. Algorithm for Text Subsets

11. make subset of the search result

12. newbie Q:create a subset

 

 
Powered by phpBB® Forum Software