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

double spaces 3

Status
Not open for further replies.

Phil Thoms

Programmer
Oct 31, 2005
245
GB
I have a field in a database (FoxPro version 6) that sometimes contains a double space within the text. The database has several thousand records. Is there a global command that will replace the double space with a single one.
 
[ ]
One way is to

[tt][blue]
SCAN
REPLACE field WITH STRTRAN(field, " ", " ")
ENDSCAN
[/blue][/tt]


That should replace all double spaces in the field with single spaces whether there is just one double space or multiple double spaces in any field. If you have triple spaces it will make them double spaces.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
To compress multiple spaces to one space for all records:

Code:
_tally=1
DO WHILE _tally>0
  REPLACE field with strtran(field, "  ", " ") for "  " $ field
ENDDO
(_tally is the number of records that the last command applied to, so as long as a " " was seen/replaced in the last run, do it again)
 
[ ]
One way to remove all multiple spaces is to do something like this:

[tt][blue]
SCAN
a = "" && empty string
tempfield = field
DO WHILE LEN(a) <> LEN(tempfield)
a = tempfield
tempfield = STRTRAN(tempfield, space(2), space(1))
ENDDO
REPLACE field WITH tempfield
ENDSCAN
[/blue][/tt]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
"sometimes contains a double space within the text"

If it is ONLY double spaces and not sometimes triple spaces (or more) then you do not need to use the SCAN/ENDSCAN or DO WHILE loops as suggested above.

You change all double spaces to a single space in one command line:
Code:
REPLACE ALL Field WITH STRTRAN(Field,"  ", " ")

The REPLACE ALL command will only change those field values where " " exists.

- OR -

Code:
REPLACE Field WITH STRTRAN(Field,"  ", " ") FOR "  " $ Field

Which has an inherent ALL when you use the FOR qualifier.
It will also only change ALL those field values where " " exists.

HOWEVER since the command runs only once it will not change triple (or more) spaces to a single space. If you have that situation, then you do indeed need to run the command multiple times.

Good Luck,
JRB-Bldr
 
Code:
Set Help to Home()+"foxtools.chm"
Help reduce
Doevents
Set Help to Home()+"dv_foxhelp.chm"

So you do
Code:
SET LIBRARY TO foxtools
Update aTable Set Field=REDUCE(Field).

Caution brigmar, in C(N) fields you typically have double,triple or more spaces: At the right end of such fields. And these trailing spaces do not change by replacing them with single spaces, the removed spaces are padded with...spaces. For example in a C(11) field:

"abc " (abc+8 spaces)
replace with "abc " (abc+4 spaces) results in
"abc " (abc+8 spaces)

You code therefore is an endless loop:

Code:
CREATE CURSOR curTest (field C(11))
INSERT INTO curTest values ('abc')
_tally=1
DO WHILE _tally>0
  REPLACE field with strtran(field, "  ", " ") for "  " $ field
ENDDO

Bye, Olaf.
 
You're right Olaf.
In fact, when I implement it, the FOR clause is actually: FOR " " $ rtrim(field)
for that very reason. I neglected that in my snippet.
 
Code:
* DBLTEST.PRG eliminate large numbers of multiple spaces with the least
* number of replace clauses and no _tally loop
clos data
set talk off
erase ("temp.dbf")
#define SPLEN 254
create table temp (spaces c(SPLEN))
for m.i=1 to SPLEN-2
  append blank
  repl spaces with "A"+space(m.i)+"B"
endfor

* Changing SPLEN from 16 to 254 only required the addition of space(16) clause
* There might be a more efficient sequence than 2,2,4,8,16
* This handles any number of spaces. Eliminate larger strtran() if the largest number of spaces is known
repl all ;
  spaces with strtran(spaces,space(16)," "), ;
  spaces with strtran(spaces,space(8)," "), ;
  spaces with strtran(spaces,space(4)," "), ;
  spaces with strtran(spaces,"  "," "), ;
  spaces with strtran(spaces,"  "," ")
count for "  "$trim(spaces)
if _tally>0 then
  ?"Fail!"
  brow noed for "  "$trim(spaces)
else
  ?"Success!"
endif
clos data
erase ("temp.dbf")
set talk on
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top