0

We have a tricky puzzle to solve in developing a scoring component. We have a C# program where we get scores from an Oracle database in the format of

Fieldname

field value

We parse them into two comma delimited strings.

We have a requirement that when we get a fieldname like "LOW@HIGH" with a field value of "11@21" we want to put it into another variable in the format of LOW=11,HIGH=21.

2 Answers 2

2

First parse the input:

var parts = input.Split('@');
var i1 = int.Parse(parts[0]);
var i2 = int.Parse(parts[1]);

Next calculate the result:

return "LOW=" + i1 + ",HIGH=" + i2;

The problem becomes easy if you break it into these two steps.

3
  • And first check with Contains() or check whether "parts" has more than 1 element.
    – Styxxy
    Commented Sep 12, 2012 at 20:31
  • 1
    @Styxxy I'd solve that by checking parts.Length. It is better to check data in its native format. String tricks are often hacks, prone to bugs and otherwise disgusting.
    – usr
    Commented Sep 12, 2012 at 20:32
  • Yeah I would do so myself, because now you have have also ABC@DCE@FGH... :).
    – Styxxy
    Commented Sep 12, 2012 at 20:58
0

And an alternative, which introduces you to the very useful Zip extension method...

string Parse(string fieldName, string fieldValue)
{
  return string.Join( ",",
           fieldName.Split( '@' )
             .Zip( fieldValue.Split( '@' ),
               ( name, value ) => string.Concat( name, "=", value ) ) );
}

all validation checks are up to you...

Note that the method works if the "field name" field contains more than two field names. For example, field name "LOW@AVERAGE@HIGH" and field value "11@15@21" would give you LOW=11,AVERAGE=15,HIGH=21

It was a quick and fun exercise but I have to wonder why it's denormalized like this?!

1
  • Because its data that was designed over 20 years ago and kludged to death ever since. We had some old C++ code that did this but it was done via memstat, pointers etc in over 60 lines of code. We were pulling our hair just trying to figure out what they did much less how to do it in C#
    – user722226
    Commented Sep 12, 2012 at 20:54

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