Subject: | |
From: | |
Reply To: | VANCE,JEFF (HP-Cupertino,ex1) |
Date: | Tue, 24 Aug 1999 23:00:11 -0600 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
Question asked:
> I'd like to reduce it to a string that
> would still contain 'n' words but where each word is
> separated by one space. 'repl' isn't exactly working....
I think I know why REPL doesn't work for you:
1 2 3
1234567890123456789012345678901
e.g. setvar a " abc def: ghi;jkl , mnop "
calc repl(a, " ", " ")
result: " abc def: ghi;jkl , mnop" (quotes added by me)
so, you need a recursive REPL function that doesn't advance to the
next character position after each replacement operation. Gavin's
suggestion of:
:SETVAR MYSTR LTRIM(RTRIM(MYSTR))
:WHILE 0 < POS(" ", MYSTR)
: SETVAR MYSTR REPL(MYSTR, " ", " ")
:ENDWHILE
is probably the most efficient and concise approach, but I haven't
measured it and compared it to the other choices.
I think it is safe to use WORD as long as you specify the delimiters
as " ". This will treat all other characters as part of the selected
word/token. Also, WORD skips blanks before and after the desired
token. So you can detect when you've hit the end of the string
passed to WORD when WORD returns "" -- valid only when a space is
the sole delimiter.
Here is another possibility, just FYI, showing an empty WHILE
loop and an obscure way of terminating the loop:
parm a
# "a" is a string that needs all spaces folded to a single space.
# Assigns the variable 'result' to the derived string.
setvar strng anyparm(a)
setvar result ""
setvar i 0
while rht(setvar(result,result+ &
word(strng," ",setvar(i,i+1))+" "),2) <> " " do
endwhile
setvar result rtrim(result)
FWIW!
Jeff Vance, CSY
|
|
|