parsing spaces to delimiter

For the Compleat Fan
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

parsing spaces to delimiter

Post by HPW »

I am a bit fusseld with this. I want to parse all spaces to an delimter.

Code: Select all

;Repeats a str with num
(define (repstr str num    newstr)
	(setq newstr "")(dotimes(x num)(setq newstr(append newstr str))))
;Replace spaces with one delimiter
(define (repspaces spacestr repstr repnum )
	(dotimes(x repnum)
	(setq spacestr (replace (repstr " " (- repnum x)) spacestr repstr))))
> a
"Test "
> (repspaces a "|" 12)

invalid function in function replace : (repstr " " (- repnum x))
called from user defined function repspaces

>

What is going wrong?
Hans-Peter

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Hi HPW,

The problem appears that you are calling a function named 'repstr' which has the same name as parameter to your function. Rename one of them and try again.

-- sam

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Oops, Thank's very much Sam!

Was a hard day in the job and I wanted to throw a new parsing function together. At least I missed the tree in the forest.

;-)
Hans-Peter

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Just a thought regarding (repspaces spacestr repstr repnum ) - if you are replacing upto repnum spaces with repstr you can use replace and the regex {min,max} matching operator thus

(define (repspaces spacestr repstr repnum )
(replace (append "\ {1," (string repnum) "}") spacestr repstr 1))

> (repspaces "Test Test Test Test" "|" 5)
"Test|Test|Test||Test"
>

(there are 6 spaces between the last two "Test"s so two separators)
The forum software font shortens spaces? spaces in the test string are
1, 3, and 6 viz "Test_Test___Test______Test"

Hope I got your intentions right

Regards
Nigel

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Or to parse all spaces to a delimter (that is any run of spaces)

use [ ]+

> (replace "[ ]+" "test TEST KKK" "|" 1)
"test|TEST|KKK"
>

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Nigel,

Thanks very much.
Yes this was my intention and the use of regex is much better.
regexp has so much power that it need's some time to fiddle out the use of it.
Hans-Peter

Locked