Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nearest match method

Status
Not open for further replies.

rsballard

Technical User
Oct 1, 2002
16
US
I have two text csv files.

in file one, field 2 is hh:mm:ss.ss and I will convert that to seconds since midnight and want to find the closest matching value in

the second csv file in which field 4 is seconds since midnight. I will then co-mingle the two lines, compute a few delta values and write it all back out to a third csv file.

there should be a match within a few seconds but I find that there is some drifting and I want to find the closest match, regardless of the variance. Don't know what I'll do yet if the data files cross midnight...(seconds since jan 1?)

I need a good method of closest matching. Recommendations?

Thanks
Bob
 
Are the files small enough to read into memory (less than a few hundred K, say)?

Read each file into a StringList. CustomSort the StringLists. Provided the hh hours fields are always two-digit numbers (e.g. 1AM would be 01:00:00.00), an alphabetic compare with CompareStr() will do just fine.

To find a value in file2, you can just roll sequentially through myStringList2, comparing each value. When you find the smallest time delta from your value in file1, that's the one you want.

Of course, this is not the fastest way; just the easiest. Faster would be to binary search through myStringList2. But code up the slow'n'simple way first; Delphi is often blindingly fast about these things, and if your data set is small enough, the extra time taken to run may not matter. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top