This is the following array (each block [] represents an entry):
[A=1] [A=5] [S=3] [A=7] [C=3] [T=2] [F=9] [Z=4] [N] [C=3] [E=8] [A=7] [N] [Z=6] [Q=1] [P=2] [Y=7] [S=3] [N]
I need to break it into objects of type "N" (NObject), where each other character represents a specific property of this NObject until the next occurrence of "N". Until the first appearance of "N" characters belong to another object (let's call it PObject). Therefore, tasks should be performed as follows:
- Map each character to the PObject property
- When the first "N" happens, create a new NObject
- Match each character property of this NObject
- If another character N occurs, create a new NObject
Currently, in pseudo-code, my solution is as follows, which I consider far from ideal.
PObject pobject = new PObject(); NObject nobject; CollectionOfKeyValuePairs collection = MyArray.Split('=').MapKeysValues() foreach(entry in collection) { switch(entry.Key): case A: (nobject ?? (CommonBase) pobject).A += entry.Value; break; case B: (nobject ?? (CommonBase) pobject).B += entry.Value; break; case C: (nobject ?? (CommonBase) pobject).C += entry.Value; break; case E: pobject.E += entry.Value; break; case F: (nobject ?? (CommonBase) pobject).F += entry.Value; break; case G: (nobject ?? (CommonBase) pobject).G += entry.Value; break; case H: (nobject ?? (CommonBase) pobject).H += entry.Value; break; ... ... ... case N: nobject = new NObject(); .... .... } }
This gives me exactly what I want:
[pobject] A = 23 B = 63 C = 23 ... [nobject] A = 34 B = 82 C = 12 ... [nobject] H = 236 K = 2 ... [nobject]
But with over 30 possible property identifiers (which means 30 switch conditions) and a property assigned only based on the fact that nobject can be null (and creating a new βNβ char event): the code is incredibly smelly. But I donβt know how to do this, maybe with the built-in functions of the collection, LINQ or something else.
source share