views:

196

answers:

2

This is a fairly trivial data parsing question. I'm just unclear on the methods I should be using to pull it off.

I've got a plain text file of a few hundred lines. Each line is of exactly the same format. The lines are in contiguous chunks where the first item in a line is essentially a key that is repeated for each line in a chunk:

key0
key0
key0

...

keyN
keyN
keyN

I would like to construct an NSDictionarys directly from this file where the lines for a given key are collapsed into a dictionary. So a dictionary of dictionarys.

Any thoughts on how to do this?

Cheers, Doug

UPDATE 0 - Here's a snapshot of the actual data Here is the actual data. I can dice the file into chunks for each chr? if need be. I'm happy with a solution that ingests a single chr.

chr1 0 2300000 p36.33 gneg

chr1 2300000 5300000 p36.32 gpos25

chr1 5300000 7100000 p36.31 gneg

chr1 7100000 9200000 p36.23 gpos25

// ... // lots more // ...

chrN 144700000 148400000 q22.3 gpos100

chrN 148400000 149600000 q23.1 gneg

chrN 149600000 150300000 q23.2 gpos25

chrN 150300000 154600000 q23.3 gneg

UPDATE 1 - The File is on disk In case I didn't make it clear, the data is on disk not memory resident. I actually think I could get away with dicing the file into pieces, one for each chr. I can then ingest into an NSArray and then on into an NSDictionary. Unless of course, someone has something snazzier.

A: 

Take a look at the NSScanner class.

Benedict Cohen
Part of the solution, but not all of it.
bbum
+1  A: 

The following solution provides a dictionary of arrays, but you can use it as a basis for producing any data structure you like:

// The values on each line are tab-delimited
NSString* data = @""
  "key1 a b"
  "key1 c d"
  "key2 e f"
  "key2 g h";

NSMutableDictionary* result = [NSMutableDictionary dictionary];  
NSArray* lines = [data componentsSeparatedByString:@"\n"];

for (NSString* line in lines) {
  NSArray* value = [line componentsSeparatedByString:@"\t"];
  NSString* key = [components objectWithIndex:0];
  [value removeObjectAtIndex:0]; // remove the key

  NSArray* currentValue = [result objectForKey:key];
  if (currentValue) {
    [currentValue addObjectsFromArray:value];
  } else {
    [result setObject:[value mutableCopy] forKey:key];
  }
}

/*

The result looks something like this:

{
  "key1": [
    ["a", "b"],
    ["c", "d"]
  ],
  "key2": [
    ["e", "f"],
    ["g", "h"]
  ]
}

*/
Nathan de Vries
Thanks for this Nathan. Actually the file is on disk rather then being memory resident. I'm an ole Perl hacker which makes this extremely easy. The key element that I'm stumped on is cleanly ingesting from disk directly into an NSDictionary.Cheers,Doug
dugla
Cool. This will definitely get me most of the way there. Thanks Nathan.
dugla
Worked like a charm. Coolness. Thanks again Nathan.
dugla
Glad it worked for you, @dulga. With regards to loading the file from disk, you can use -[NSString stringWithContentsOfFile:] to do that.
Nathan de Vries