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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global replace of specific characters (DBASE III+/IV)

Status
Not open for further replies.

geosta

Technical User
Feb 7, 2006
3
GR
I want to write a program in DBASE III+ or IV which will search all fields in all records of a file named "Myfile.dbf" and will replace every "G" anywhere in the record with "S" in place of "G", leaving all other characters unchanged. It will convert "Bigger" to "Bisser" and "George" to "Seorge"!! etc. It must close the file after completing the task.
I know how to make simple programs to add records, print etc. in dBASE but I cannot find a solution to the above problem. I suspect that it is not very difficult. Could you please help? thank you.
 
Try this:
*MYFIELD IS THE DATAFIELD TO BE CORRECTED
*MYFIELD = SPACE(6)
*MYFIELD = "BIGGER"
NR1 = 1 (BEGINNING CHARACTER IN MYFIELD)
NR2 = 1 (NR CHARACTERS TO READ IN MYFILE)
CC = SPACE(1)
CCC = SPACE(6)
USE MYFILE.DBF
GO TOP
DO WHILE NR < 7
CC = SUBSTR(MYFIELD, NR1, NR2)
IF CC = &quot;G&quot;
CCC = TRIM(CCC) + &quot;S&quot;
ENDIF
IF CC <> &quot;G&quot;
CCC = TRIM(CCC) + SUBSTR(MYFIELD, NR1, NR2)
ENDIF
NR1 = NR1 + 1
CC = &quot; &quot;
LOOP
ENDDO
CLOSE DATABASES
? CCC (SHOULD READ BISSER)

I am rayleo@erols.com


 
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 &quot; &quot; TO MSER,MREP,MCONF
DO WHILE .NOT. MCONF $ &quot;YQ&quot;
@ 10,10 SAY &quot;Enter the character you wish to find ...&quot; GET MSER
@ 11,10 SAY &quot;Enter the character you wish to insert..&quot; GET MREP
@ 14,10 SAY &quot;Enter # in the character fields to end the run.&quot;
READ
IF .NOT. (MREP=&quot;#&quot; .OR. MSER=&quot;#&quot;)
@ 12,10 SAY &quot;Find &quot;+MSER+ &quot; and replace it with &quot;+MREP+&quot; &quot; GET MCONF PICTURE &quot;@! Y&quot;
@ 13,10 SAY &quot;(Enter Y if values are correct, N if they are not.)&quot;
@ 14,0 CLEAR TO 14,79
READ
ENDIF
* searching & replacing identical characters filter
IF (MSER = MREP) .AND. (.NOT. MREP $ &quot; #&quot;)
@ 15,10 SAY &quot;Searching & replacing identical values is invalid&quot;
STORE &quot;N&quot; TO MCONF
LOOP
ENDIF
IF MCONF = &quot;Y&quot; .OR. (MREP=&quot;#&quot; .OR. MSER=&quot;#&quot;)
EXIT
ELSE
@ 12,0 CLEAR TO 14,79
ENDIF
ENDDO
* testing for finish of process
IF (MREP= &quot;#&quot; .OR. MSER = &quot;#&quot;)
EXIT
ENDIF
@ 6,4 SAY &quot;Now working with...&quot;
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))=&quot;C&quot;
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 &quot;All &quot; + MSER +&quot; replaced with &quot; + MREP + &quot;.&quot;
ENDDO

 
Dear geosta,
Wow
– When I read ‘rayleo’s reply I thought the guy was a super-genius – accomplishing the task in 23 lines of code and not 60 as in my attempt. Then I realised that his code only replaces the sought character in one specified field and only for one record. Phew!. I thought I’d got the question totally wrong BUT you did say to search for a character in all fields and in all records and replace it. Nevertheless, the rayleo’s approach is a valid method of tackling the problem. My concern is that the application sort of tests each character incrementally and I think on large files this might prove to be a slow method – but hey – if it gets the job done.
Looking at my hastily knocked up code, it could be improved by giving the operator the chance to skip fields if required – code follows.

Code:
  STORE FIELD(COUNTER) TO MFIELD (Line 42 in original code)
  @ 6,25 SAY MFIELD + SPACE(16)  (Line 43 in original code)
***New lines of code to skip fields at operators say so
***Yes processes the field, N skips it
  STORE &quot; &quot; TO MCONF2
  @ 18,10 SAY &quot;Do you wish to process the field &quot; + MFIELD GET MCONF2 PICTURE &quot;@! Y&quot;
  READ
  IF MCONF2 = &quot;N&quot;
   LOOP
  ENDIF
** resumes to previous code
  DO WHILE .NOT. EOF() (Line 44 in original code)
Errors and Ommissions. In my code, talk, echo and step are all set to off.
(Come to think of it rayleo is probably a super-genious anyhoo!)
Good Luck
GEMS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top