Reputation: 9
I am trying to isolate elements in an array. I have an NSArray containing over 100000 NSNumber Objects. The problem I have is they are arranged by increments of 3.
processarray = [[NSMutableArray alloc] init];
for (loopstart = 0; loopstart < arraycount; loopstart ++)
{
arrayvalue = [[mesharray objectAtIndex: loopstart] floatValue];
coordinate = [NSNumber numberWithFloat: arrayvalue];
[processarray addObject: coordinate];
[printgcodetoconsole appendFormat: @"\nCOORDINATE: %@", coordinate];
}
For Example, my array looks like this:
What I need is a way to group the correct values together in terms of X, Y, and Z because they are points in space like:
and so on…
There are no similarities between the objects in the array except that they are all NSNumber objects.
Thanks In Advance!
I am currently printing this array as strings in a NSTextView and I can't figure out how to isolate the strings without messing up the loop with this code:
[printgcodetoconsole appendFormat: @"\nCOORDINATE: %@", coordinate];
I would really prefer something more like:
NSNumber * X;
NSNumber * Y;
NSNumber * Z;
[printgcodetoconsole appendFormat: @"\n X%@, Y%@, Z%@", X, Y, Z];
Upvotes: 0
Views: 221
Reputation: 9
Thanks a lot guys… the answer turned out to be a variation on JeroValli's idea about using a modulo. It's a damn sexy idea I might add. Thanks Jero and everyone else involved.
for (loopstart = 0; loopstart < arraycount; loopstart ++)
{
arrayvalue = [[mesharray objectAtIndex: loopstart] floatValue];
point = [NSNumber numberWithFloat: arrayvalue];
[coordinates addObject: point];
if (loopstart % 3 == 0)
{
[printgcodetoconsole appendFormat: @"\nCOORDINATE: X%@", point];
}
else if (loopstart % 3 == 1)
{
[printgcodetoconsole appendFormat: @"\nCOORDINATE: Y%@", point];
}
else if (loopstart % 3 == 2)
{
[printgcodetoconsole appendFormat: @"\nCOORDINATE: Z%@", point];
}
Upvotes: 0
Reputation: 95335
This simple code will take every three items from meshArray, put them into a new array and insert that into a larger array (so that you have an array of arrays).
NSArray *meshArray = /* obtain from somewhere */;
NSAssert(([meshArray count] % 3) == 0, @"Array does not contain multiple of 3");
NSMutableArray coordinates = [NSMutableArray array];
for (NSUInteger i = 0; i < [meshArray count]; i += 3)
[coordinates addObject:[meshArray subarrayWithRange:NSMakeRange(i, 3)]];
Alternatively, if you want, you can key them using a dictionary:
NSArray *meshArray = /* obtain from somewhere */;
NSAssert(([meshArray count] % 3) == 0, @"Array does not contain multiple of 3");
NSMutableArray coordinates = [NSMutableArray array];
for (NSUInteger i = 0; i < [meshArray count]; i += 3)
{
NSDictionary *coordinate = @{ @"X" : meshArray[i], @"Y" : meshArray[i + 1], @"Z" : meshArray[i + 2] };
[coordinates addObject:coordinate];
}
Upvotes: 0
Reputation: 569
You should check when the counter loopstart
is multiple of 3 and group the last 3 values on a sub-array:
NSMutableArray *subArray = [NSMutableArray array];
for (loopstart = 0; loopstart < arraycount; loopstart ++) {
arrayvalue = [[mesharray objectAtIndex:loopstart] floatValue];
coordinate = [NSNumber numberWithFloat: arrayvalue];
[subArray addObject:coordinate];
if (loopstart % 3 == 0) {
[processarray addObject:subArray];
subArray = [NSMutableArray array];
}
}
Upvotes: 1