Dear Jeff,
From chatgpt:
1. Load the CSV into a Memory Array
Read the .csv file into a memory array or hash table. This allows quick lookups by cOld and cName.
2. Iterate Over the Database Efficiently
Use a single loop through your database and check for matches in the array or hash table instead of iterating over the .csv file repeatedly.
3. Batch Updates
When a match is found, update MyDB->ID with cNew directly.
- Code: Select all Expand view RUN
FUNCTION UpdateDatabase()
LOCAL aCSV := {}, cLine, nFileHandle, hTable := {}, cCompositeKey
// Open the database
USE MyDB EXCLUSIVE NEW
// Load CSV into memory
nFileHandle := FOpen("update_file.csv")
IF nFileHandle == -1
? "Failed to open update_file.csv"
RETURN
ENDIF
WHILE ! FEOF(nFileHandle)
cLine := AllTrim(FReadLine(nFileHandle))
IF Empty(cLine)
LOOP
ENDIF
// Parse CSV line
LOCAL aFields := HB_ATokens(cLine, ",")
AAdd(aCSV, { aFields[1], aFields[2], aFields[3] }) // cOld, cNew, cName
// Create a hash table for quick lookup
cCompositeKey := aFields[1] + "_" + aFields[3]
hTable[cCompositeKey] := aFields[2] // Map composite key to cNew
ENDDO
FClose(nFileHandle)
// Traverse the database
GO TOP
DO WHILE ! EOF()
cCompositeKey := MyDB->ID + "_" + MyDB->Name
IF HB_HHasKey(hTable, cCompositeKey)
MyDB->ID := hTable[cCompositeKey]
// Optional: Mark the record as updated if needed
// REPLACE MyDB->Updated WITH .T.
ENDIF
SKIP
ENDDO
// Clean up and close
CLOSE DATABASES
RETURN
Key Improvements in This Approach
1. Hash Table Lookup
Instead of searching the .csv line by line, the hash table enables O(1) lookups for each database record, massively improving performance.
2. Single Pass Through Database
Only one loop is needed for the database, avoiding the costly nested loop.
3. Minimal Disk I/O
The .csv is read into memory entirely at the start, reducing repetitive file reads.