for Loop Error or Logical Error?
for Loop Error or Logical Error?
- Subject: for Loop Error or Logical Error?
- From: "Matthew Callis" <email@hidden>
- Date: Sat, 23 Dec 2006 14:23:56 -0600
I have a loop that is going through an array of sorted items and making a
list of the unique items while counting the instances of them. The error
occurs in that the last unique set of items in the array is not being
counted or listed, even though It IS in the array. The output should have 3
items, not 2.
WRONG Output:
Radiohead Bonnaroo Music & Arts Festival Manchester, Tennessee
Alternative 2006-06-17 Edirol R4 2:27:23 29
Radiohead Bonnaroo Music & Arts Festival Manchester, Tennessee
Alternative 2006-06-17 Matrix 2:25:09 28
RIGHT Output:
Radiohead Bonnaroo Music & Arts Festival Manchester, Tennessee
Alternative 2006-06-17 Edirol R4 2:27:23 29
Radiohead Bonnaroo Music & Arts Festival Manchester, Tennessee
Alternative 2006-06-17 Matrix 2:25:09 28
Radiohead Bonnaroo Music & Arts Festival Manchester, Tennessee
Alternative 2006-06-17 Sony TCD-D8 2:27:23 28
The Loop:
NSString *currentArtist = [[allTapes objectAtIndex:0] artist];
NSString *currentVenue = [[allTapes objectAtIndex:0] venue];
NSString *currentCityState = [[allTapes objectAtIndex:0] citystate];
NSString *currentGenre = [[allTapes objectAtIndex:0] genre];
NSString *currentDate = [[allTapes objectAtIndex:0] date];
NSString *currentSource = [[allTapes objectAtIndex:0] source];
int currentLength = 0;
int currentCount = 0;
int albumCount = 0;
int totalTime = 0;
NSMutableString *tapeBunch;
tapeBunch = [[NSMutableString alloc] init];
int allofthem = [allTapes count];
for(i = 0; i < allofthem; i++){
NSComparisonResult artistResult, venueResult, cityResult,
dateResult, sourceResult;
NSString *thisArtist = [[allTapes objectAtIndex:i] artist];
NSString *thisVenue = [[allTapes objectAtIndex:i] venue];
NSString *thisCity = [[allTapes objectAtIndex:i] citystate];
NSString *thisDate = [[allTapes objectAtIndex:i] date];
NSString *thisSource = [[allTapes objectAtIndex:i] source];
artistResult = [thisArtist compare:currentArtist];
venueResult = [thisVenue compare:currentVenue];
cityResult = [thisCity compare:currentCityState];
dateResult = [thisDate compare:currentDate];
sourceResult = [thisSource compare:currentSource];
if( (artistResult != NSOrderedSame) || (dateResult !=
NSOrderedSame) || (sourceResult != NSOrderedSame) || (venueResult !=
NSOrderedSame) || (cityResult != NSOrderedSame)){
[tapeBunch appendString: currentArtist];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: currentVenue];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: currentCityState];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: currentGenre];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: currentDate];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: currentSource];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: [NSString
stringWithFormat:@"%i:i:i",
(currentLength / 60) / 60, (currentLength / 60) % 60, currentLength % 60]];
[tapeBunch appendString: @"\t"];
[tapeBunch appendString: [NSString stringWithFormat:@"%i",
currentCount]];
[tapeBunch appendString: @"\n"];
albumCount++;
totalTime += currentLength;
currentArtist = [[allTapes objectAtIndex:i] artist];
currentVenue = [[allTapes objectAtIndex:i] venue];
currentCityState = [[allTapes objectAtIndex:i] citystate];
currentGenre = [[allTapes objectAtIndex:i] genre];
currentDate = [[allTapes objectAtIndex:i] date];
currentLength = [[allTapes objectAtIndex:i] length];
currentSource = [[allTapes objectAtIndex:i] source];
currentCount = 1;
}
else{
currentCount++;
currentLength += [[allTapes objectAtIndex:i] length];
}
}
--
- Matthew
http://eludevisibility.org/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden