[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Subject: Re: parsing a string

William James

8/11/2015 12:17:00 PM

Wade Humeniuk wrote:

> Here is one possible way
>
> (defun parse ($string tokens)
> (loop with start = 0
> for (first next) on tokens
> for start1 = (+ (search first $string :test #'string= :start2 start)
> (length first) 1)
> for end1 = (when next (search next $string :test #'string= :start2 star
> t1))
> collect (prog1 (cons first (subseq $string start1 (when end1 (1- end1))
> ))
> (setf start end1))))
>
> CL-USER 12 > (parse "test$foo$123$name$empty$$last$bar" '("foo" "empty" "last"))
> (("foo" . "123$name") ("empty" . "") ("last" . "bar"))


What if the tokens are in a different order?

Testing:

CL-USER(5): (parse "test$foo$123$name$empty$$last$bar" '("last" "foo" "empty"))
Debugger invoked on condition of type TYPE-ERROR:
The value NIL is not of type REAL.



Gauche Scheme:

(use srfi-1 :only (break))

(define (parse $string tokens)
(let go ((words (find-tail (cut member <> tokens) (string-split $string #\$))))
(if (pair? words)
(receive (data more) (break (cut member <> tokens) (cdr words))
(cons (cons (car words) (string-join data "$")) (go more)))
'())))

gosh> (parse "test$foo$123$name$empty$$last$bar" '("foo" "empty" "last"))
(("foo" . "123$name") ("empty" . "") ("last" . "bar"))
gosh> (parse "test$foo$123$name$empty$$last$bar" '("last" "foo" "empty"))
(("foo" . "123$name") ("empty" . "") ("last" . "bar"))


--
The struggle of our time is to concentrate, not to dissipate: to renew our
association with traditional wisdom: to re-establish a vital connection between
the individual and the race. It is, in a word, a struggle against Liberalism.
--- T. S. Elliot