2

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!

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What type of object would states be?
@Nic: An NSMutableDictionary with the state codes being the key.
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: 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.
1

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.

2 Comments

Dave, I am using componentsSeparatedByString.
@Nic great; now follow the principles of object oriented programming and encapsulate this data in its own object.
0

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.

1 Comment

Use the 101 button or indent by 4 spaces for code markup. Most of it looks surprisingly similar to a certain answer above though ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.