parsing spaces to delimiter

Started by HPW, April 22, 2004, 08:03:17 AM

Previous topic - Next topic

HPW

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


 
;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

#1
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

#2
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

#3
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

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



use [ ]+



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

"test|TEST|KKK"

>

HPW

#5
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