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.

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

3 Comments

And first check with Contains() or check whether "parts" has more than 1 element.
@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.
Yeah I would do so myself, because now you have have also ABC@DCE@FGH... :).
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 Comment

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#

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.