1 Reply Latest reply: Aug 8, 2009 2:07 AM by 800282 RSS

    Combination of 2 Dynamic Arrays

    843853
      Hi everyone,

      Lets say that we have two dynamic arrays where the user first defines their length and after that he inserts the values.
      For example we have the arrays "Locations" and "Projects". The user defines that the length of "Locations" will be 2 and the length of "Projects" will be 3, and after that he inserts some values in each array, so lets say we have:

      Locations["Italy", "Germany"]
      Projects["School", "House", "Bar"]

      I need an output like this:

      (Italy: School)
      (Italy: House)
      (Italy: Bar)
      (Italy: School, House) *
      (Italy: School, Bar) *
      (Italy: House, Bar) *
      (Italy: School, House, Bar) *
      (Germany: School)
      (Germany: House)
      (Germany: Bar)
      (Germany: School, House) *
      (Germany: School, Bar) *
      (Germany: House, Bar) *
      (Germany: School, House, Bar) *

      * It doesn't matter the order of the Locations if there are 2 or more

      Thanks for your time.
        • 1. Re: Combination of 2 Dynamic Arrays
          800282
          First generate all combinations of your Projects-array. Observe that this is nothing more than counting from 1 to (2^n)-1 where n is the size of your array:
          .
              "School"  "House"  "Bar"
          1       0        0       1    ->   ["Bar"]
          2       0        1       0    ->   ["House"]
          3       0        1       1    ->   ["House", "Bar"]
          4       1        0       0    ->   ["School"]
          5       1        0       1    ->   ["School", "Bar"]
          6       1        1       0    ->   ["School", "House"]
          7       1        1       1    ->   ["School", "House", "Bar"]
          Bow simply iterate over your Locations-array and combine each entry to all of the Projects-combinations.
          Note that I assume that this is some sort of school/homework project and that the sizes of the arrays will stay relatively small. Generating (an storing) all combinations of large arrays is a no-go.

          Good luck.