in reply to Combinations of a set of elements

This smacks of homework, but:
my @set = qw(A B C); my @list; foreach my $one (@set) { foreach my $two (@set) { push @list,"${one}$two"; } }


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: Re: Combinations of a set of elements
by shemp (Deacon) on Nov 13, 2002 at 21:47 UTC
    This is called a cartesian product incidentally.
Re: Re: Combinations of a set of elements
by pg (Canon) on Nov 13, 2002 at 20:59 UTC
    This will have a problem, if the set is ("a", "a", "a", "a", "b"), then you will repeat combinations unnecessarily.

      Sets, by definition, don't have duplicate elements.

      Collections which permit duplicate elements are known as "bags" or "multisets."

      -sauoq
      "My two cents aren't worth a dime.";
      
      Maybe not. It depends on whether the duplicates are significant or not. That wasn't specified in the question.

      -pete
      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."