At 18:36 -0600 23/2/09, Ashley Perrien wrote:
This is relatively easy if I know how many arrays I'm working with (3 in this case) to simply nest the for loops but if I don't know how many arrays the primary array has, I can't think of a way to nest the loops if I don't know how deeply to nest them. Any suggestions on the best way to approach something like this?

You could use a simple recursion, something like this:

void Recurse( NSMutableArray* allresults, NSArray* arrays, int index, NSMutableArray* current )
{
  if ( index >= [arrays count] ) {
    [allresults addObject:current];
  } else {
    for ( id obj in [arrays objectAtIndex:index] ) {
      NSMutableArray* c = [current mutableCopy];
      [c addObject:obj];
      Recurse( allresults, arrays, index+1, c );
      [c release];
    }
  }
}

void Doit()
{
  // Initialize the 2D array
  NSMutableArray *arrays = [NSMutableArray array];
  [arrays addObject:[NSArray arrayWithObjects:@"1", @"2", @"3", nil]];
  [arrays addObject:[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil]];
  [arrays addObject:[NSArray arrayWithObjects:@"Red", @"White", @"Blue", nil]];

  NSMutableArray *allresults = [NSMutableArray array];
  Recurse( allresults, arrays, 0, [NSMutableArray array] );
NSLog( @"%@", allresults );
}

Enjoy,
   Peter.

--
     Run macros from your iPhone with Keyboard Maestro Control!
       or take a break with Aragom Space War for your iPhone

Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
Aragom Space War <http://www.stairways.com/iphone/aragom> Don't get killed!
<http://www.stairways.com/>           <http://download.stairways.com/>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to