Comparing & Finding changes

Started by
2 comments, last by LuckyOne 17 years, 10 months ago
Hi, on paper my problem is easy to fix, but in reality i have only little idea about how to do this. So this is what i have
string olddata = "100,354,999,123,555";
string newdata = "100,200,123,234";
any ideas about how to find out which values are deleted and which are added? i expect to see something like this as result:
string added = "200,234";
string removed = "354,999,555";
Any help would be appreciated :) p.s. im using c#
Advertisement
I don't know c#, but I imagine the general procedure would be:

1)Convert the old and new lists of numbers from one long string into tokens. There are probably library functions for this.

2)Convert those tokens into integers. I'm sure there's some type of container like a vector in c#, so use that to store the tokens in.

3)Sort the containers

for your example you would have something like this right now:
oldNumbers newNumbers  100        100  123        123  354        200  555        234  999

4)Create two indices, one for each container

5)Create two new containers to store the added and removed data. You can probably just go directly from integers to strings here

6)Compare the values from above for each case:
a)if both numbers are equal, incrememnt both indices
b)if oldNumbers > newNumbers add the number from newNumbers into the 'added numbers' container. Then increment the index for newNumbers
c)if oldNumbers < newNumbers add the number from oldNumbers into the 'removed numbers' container. Then increment the index for oldNumbers
d)if one container is empty, just output the non-empty container into the appropriate add or remove container

I think that will do it. Hopefully I didn't leave out any steps.

EDIT: case D is important too!

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

First, I assume that you are able to split your strings into individual strings that represent your numbers. Then the algorithm is rather simple:
olddatacpy = olddatafor each number N in newdata  if N is in olddatacpy {    remove N from olddatacpy  } else {    add N to added  }}removed = olddatacpy

If you need a more general diff algorithm, it will be trickier :)

HTH,
Finally, Got it!

Thanks, for the help! :D

This topic is closed to new replies.

Advertisement