Hi,
This one has me stumped. I am trying to set up a script that will perform a STRTRAN() on a target field in the master table (WorkFile) using a set of values contained in a two-field transaction table called Replace. The target field in the master table is, ADD2, and the two fields in the transaction table are, FIND (the search string) and REPLACE (the replacement string).
Although I have gotten it to work using WHILE� END WHILE statements which cycle through the transaction table using the sets of values on a record by record basis, applying a STRTRN() statement for each, then replacing the ADD2 field�s content with the result, it takes a long time. If you have 200-500 records in the transaction table�
So, it seemed to me that by copying all of the transaction table�s values into two arrays (Finds[1000] and Replaces[1000]) that could be used in the STRTRAN() function over and over again, the operation would run much faster using these memory variables as the operation wouldn�t have to take extra time to re-read all of the transaction table�s records every time a master table record is reviewed/updated.
I wrote a script that runs (see below), but it only searches and replaces the last value in each of the arrays when it updates the master table�s records.
The problem seems to arise from the FOR� NEXT statement, but I can�t tell what it is. INSIGHT, is what�s needed here, but I am apparently fresh out, and would greatly appreciate any that might be offered�
Thanks,
Geoff Hollander
CODE:
''XBasic
DIM tbl as P
DIM srch as P
DIM counter as N
DIM i as N
DIM number_srch_recs as N
DIM global Finds[1000] as C
DIM global Replaces[1000] as C
counter = 0
tbl = table.open(WorkFile)
srch = table.open(Replace)
number_srch_recs = srch.records_get()
srch.batch_begin()
srch.fetch_first()
WHILE .not. srch.fetch_eof()
counter = counter + 1
find_str = srch.find
replace_str = srch.replace
Finds[counter] = alltrim(find_str)
Replaces[counter] = alltrim(replace_str)
srch.fetch_next()
END WHILE
srch.batch_end()
tbl.batch_begin()
tbl.fetch_first()
WHILE .not. tbl.fetch_eof()
FOR i = 1 TO number_srch_recs
field_old = tbl.add2
replacement = strtran(field_old, Finds[i], Replaces[i])
NEXT i
tbl.change_begin()
tbl.add2 = replacement
tbl.change_end(.t.)
tbl.fetch_next()
END WHILE
tbl.batch_end()
END
This one has me stumped. I am trying to set up a script that will perform a STRTRAN() on a target field in the master table (WorkFile) using a set of values contained in a two-field transaction table called Replace. The target field in the master table is, ADD2, and the two fields in the transaction table are, FIND (the search string) and REPLACE (the replacement string).
Although I have gotten it to work using WHILE� END WHILE statements which cycle through the transaction table using the sets of values on a record by record basis, applying a STRTRN() statement for each, then replacing the ADD2 field�s content with the result, it takes a long time. If you have 200-500 records in the transaction table�
So, it seemed to me that by copying all of the transaction table�s values into two arrays (Finds[1000] and Replaces[1000]) that could be used in the STRTRAN() function over and over again, the operation would run much faster using these memory variables as the operation wouldn�t have to take extra time to re-read all of the transaction table�s records every time a master table record is reviewed/updated.
I wrote a script that runs (see below), but it only searches and replaces the last value in each of the arrays when it updates the master table�s records.
The problem seems to arise from the FOR� NEXT statement, but I can�t tell what it is. INSIGHT, is what�s needed here, but I am apparently fresh out, and would greatly appreciate any that might be offered�
Thanks,
Geoff Hollander
CODE:
''XBasic
DIM tbl as P
DIM srch as P
DIM counter as N
DIM i as N
DIM number_srch_recs as N
DIM global Finds[1000] as C
DIM global Replaces[1000] as C
counter = 0
tbl = table.open(WorkFile)
srch = table.open(Replace)
number_srch_recs = srch.records_get()
srch.batch_begin()
srch.fetch_first()
WHILE .not. srch.fetch_eof()
counter = counter + 1
find_str = srch.find
replace_str = srch.replace
Finds[counter] = alltrim(find_str)
Replaces[counter] = alltrim(replace_str)
srch.fetch_next()
END WHILE
srch.batch_end()
tbl.batch_begin()
tbl.fetch_first()
WHILE .not. tbl.fetch_eof()
FOR i = 1 TO number_srch_recs
field_old = tbl.add2
replacement = strtran(field_old, Finds[i], Replaces[i])
NEXT i
tbl.change_begin()
tbl.add2 = replacement
tbl.change_end(.t.)
tbl.fetch_next()
END WHILE
tbl.batch_end()
END
Comment