Page 1 of 1

parsing spaces to delimiter

Posted: Thu Apr 22, 2004 3:03 pm
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?

Posted: Thu Apr 22, 2004 3:29 pm
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

Posted: Thu Apr 22, 2004 3:41 pm
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.

;-)

Posted: Fri Apr 23, 2004 12:55 am
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

Posted: Fri Apr 23, 2004 2:43 am
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"
>

Posted: Fri Apr 23, 2004 6:31 am
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.