[REBOL] Re: newlines
From: joel:neely:fedex at: 7-Nov-2001 0:29
Jim Clatfelter wrote:
> Is there any reason this should not work to be sure there are
> no more than two newlines in a row in the text? It works fine
> except for the top of the page. It won't even work if I change
> it to 'loop 200. It leaves one place with 4 newlines in a row.
> Maybe there's another character that forces a line feed?
>
> loop 20 [replace/all my-area/text "^/^/^/" "^/^/"]
>
I just did a quick test that seems to confirm that it should work.
Here are some suggestions:
1) Verify that there isn't some other nonprinting character (e.g.,
a tab, carriage-return, etc.) within a run of newlines. For
example:
replace/all my-area/text "^/ ^/" "^/^/"
replace/all my-area/text "^/^-^/" "^/^/"
;...
2) Use TRIM and TRIM/ALL to remove leading and trailing whitespace.
3) Speed up the process by eliminating long runs first.
replace/all my-area/text "^/^/^/^/^/^/^/^/^/^/" "^/^/"
replace/all my-area/text "^/^/^/^/^/^/" "^/^/"
replace/all my-area/text "^/^/^/^/" "^/^/"
replace/all my-area/text "^/^/^/" "^/^/"
4) Only repeat the REPLACE/ALL enough to get the job done. The
code below illustrates (using dots instead of newlines to
make the action visible). First we construct a test string
with lots of runs of dots:
>> s: ""
== ""
>> loop 200 [append s either 1 = random 20 ["x"] ["."]]
== {.x.x...........x.x.................x....xx.x..........
..........................................................
............x.....
then we take out runs of more than two dots:
>> newlen: oldlen: length? s
== 200
>> until [
[ oldlen: newlen
[ oldlen = newlen: length? replace/all s "..." ".."
[ ]
== true
>> s
== ".x.x..x.x..x..xx.x..x..x..x.x..x.."
by repeatedly applying REPLACE/ALL until the result has the
same length as the previous length.
Hope this helps!
-jn-
--
'Tis an ill wind that blows no minds.
-- Anonymous
joel[dot[neely[at[fedex[dot[FIX[PUNCTUATION[com