Dear geosta
This is a long reply so I'll try & be brief.
The following dbIII+ application will search and replace characters within a dbase file that the operator selects. It's a little rigid in that it's case specific. For example, to replace all occurrences of the letter ‘a’ with the letter ‘b’ the operator has to replace first ‘a’ with ‘b’ then ‘A’ with ‘B’.
Note that the S&R facility restricts itself to character fields.
As a search and replace facility, the normal precautions should be taken. Eg., think twice about replacing a space with a character – all spaces will be replaced! Similarly, exchanging characters has to be three stage process. It is always advisable to perform S&R operations on a copy table to test the results.
The application makes use of the following functions;
AT(x,y) function which finds the position of x in y
STUFF(EXPC1,EXPN1,EXPN2,EXPC2) which replaces EXPC1 with EXPC2 for EXPN1 characters starting at position EXPN2 in EXPC1. Eg Stuff(“Alligator”,3,1,”Dog”) gives “Dogigator”
TYPE(FIELD(N)) which yields “C”, or “N” or “D” or “L”
FIELD (N) where n is a number – this yields the fieldname and is useful for cycling through fields.
Storing FIELD(N) to MFIELD and then calling up &MFIELD is the only way I know of getting field names and replacing their contents. Anyone any better ideas?
The application is basic with no frills or bells and has only rudimentary error checking. But then that’s the challenge, you can make it sing! Clipboard the file into WordPad and the line ends should be observed correctly.
Good luck.
:
CLEAR
USE C:\TEMP\MYFILE
* edit that line as you wish
DO WHILE .T.
GOTO TOP
*counter allows us to track through all the fields
STORE 0 TO COUNTER
* setting the search & replace characters
@ 12,0 CLEAR TO 14,79
STORE " " TO MSER,MREP,MCONF
DO WHILE .NOT. MCONF $ "YQ"
@ 10,10 SAY "Enter the character you wish to find ..." GET MSER
@ 11,10 SAY "Enter the character you wish to insert.." GET MREP
@ 14,10 SAY "Enter # in the character fields to end the run."
READ
IF .NOT. (MREP="#" .OR. MSER="#"
@ 12,10 SAY "Find "+MSER+ " and replace it with "+MREP+" " GET MCONF PICTURE "@! Y"
@ 13,10 SAY "(Enter Y if values are correct, N if they are not.)"
@ 14,0 CLEAR TO 14,79
READ
ENDIF
* searching & replacing identical characters filter
IF (MSER = MREP) .AND. (.NOT. MREP $ " #"
@ 15,10 SAY "Searching & replacing identical values is invalid"
STORE "N" TO MCONF
LOOP
ENDIF
IF MCONF = "Y" .OR. (MREP="#" .OR. MSER="#"
EXIT
ELSE
@ 12,0 CLEAR TO 14,79
ENDIF
ENDDO
* testing for finish of process
IF (MREP= "#" .OR. MSER = "#"
EXIT
ENDIF
@ 6,4 SAY "Now working with..."
DO WHILE .T.
GOTO TOP
COUNTER=COUNTER+1
IF LEN(FIELD(COUNTER))=0
EXIT
ENDIF
STORE FIELD(COUNTER) TO MFIELD
@ 6,25 SAY MFIELD + SPACE(16)
DO WHILE .NOT. EOF()
* restricting search & replace to character fields
IF TYPE(FIELD(COUNTER))="C"
LOCATE REST FOR MSER $ &MFIELD
IF FOUND()
* the next loop catches repeats of a character within a field
DO WHILE .T.
REPLACE &MFIELD WITH STUFF(&MFIELD,AT(MSER,&MFIELD),1,MREP)
IF AT(MSER,&MFIELD)>0
LOOP
ELSE
EXIT
ENDIF
ENDDO
ENDIF
ELSE
EXIT
ENDIF
ENDDO
ENDDO
@ 6,32 SAY "All " + MSER +" replaced with " + MREP + "."
ENDDO