up vote 1 down vote favorite

I have an array of strings that are comma separated such as:

Steve Jobs,12,CA
Fake Name,21,CA
Test Name,22,CA
Bill Gates,44,WA
Bill Nye,21,OR

I have those values in an NSScanner object so that I can loop through the values and get each comma seperated value using objectAtIndex.

So, what I would like to do, is group the array items into new arrays, based on a value, in this case, State. So, from those, I need to loop through, checking which state they are in, and push those into a new array, one array per state.

CA Array:
Steve Jobs,12,CA
Fake Name,21,CA
Test Name,22,CA

WA Array:
Bill Gates,44,WA

OR Array:
Bill Nye,21,OR

So in the end, I would have 3 new arrays, one for each state. Also, if there were additional states used in the first array, those should have new arrays created also.

Any help would be appreciated!

link|flag

78% accept rate

3 Answers

up vote 2 down vote accepted

You can use a NSMutableDictionary of NSMutableArrays - if the state encountered isn't yet in the dictionary, add a new array.

NSMutableArray* arr = [states objectForKey:state];
if (arr == nil) {
    arr = [NSMutableArray array];
    [states setObject:arr forKey:state];
}

Then you can insert values into the array, preferably as objects though as Dave DeLong mentions.

link|flag
@Dave: Thanks for the fix. – Georg Fritzsche May 4 at 16:58
What type of object would states be? – Nic Hubbard May 4 at 17:01
@Nic: An NSMutableDictionary with the state codes being the key. – Georg Fritzsche May 4 at 17:04
Ok, I made a new class and put put each one of my objects into an NSMutableArary. Will this make it easer? I did similar to what you posted here: stackoverflow.com/questions/2766994#2767037 – Nic Hubbard May 4 at 18:12
@Nic: That was Daves answer, not mine. Using costum objects makes it much easier if you use the data in more then one place - you can just access it via the given names instead of repeating your parsing code etc. Also, repeatedly parsing the same information has a performance penalty. – Georg Fritzsche May 4 at 19:29
up vote 1 down vote

You shouldn't be maintaining this data as CSV. That's asking for a world of hurt if you ever need to manipulate this data programmatically (such as what you're trying to do).

You can naïvely break this data up into an array using NSArray * portions = [line componentsSeparatedByString:@","];. Then create a custom object to store each portion (for an example, see this post), and then you can manipulate those objects almost effortlessly.

link|flag
Dave, I am using componentsSeparatedByString. – Nic Hubbard May 4 at 16:55
1  
@Nic great; now follow the principles of object oriented programming and encapsulate this data in its own object. – Dave DeLong May 4 at 17:11
up vote 0 down vote

Naively: (assuming array of strings called strings)

NSMutableDictionary *states = [NSMutableDictionary dictionary];
for (NSString *string in strings) {
  NSString *state = [[string componentsSeparatedByString:@", "] lastObject];  
  NSMutableArray *values = [states objectForKey:state];
  if (values == nil) {
     values = [NSMutableArray array];
     [states setObject:value forKey:state];
  }
  [values addObject:string];
}

Number of things about this -- first of all, I'm not at my computer, so there is a high chance of typos and or things that I missed. Second, you probably want to adapt the components separated by string line to handle whitespace better.

link|flag
Use the 101 button or indent by 4 spaces for code markup. Most of it looks surprisingly similar to a certain answer above though ;) – Georg Fritzsche May 4 at 17:23

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.