destructive append to a string

Started by Dmi, December 17, 2005, 06:16:34 AM

Previous topic - Next topic

Dmi

Is there a way to destructive append to the end of string?

Similar to 'push?
WBR, Dmi

HPW

#1
Use:



From the help-file:

;; fast in-place string appending
(set 'str "")
(dotimes (x 5) (write-buffer str "hello"))
Hans-Peter

Dmi

#2
Oh! Thanks!
WBR, Dmi

Lutz

#3
... and 'write-line' works too for destructive string append.



About your XML questions/example. I don't see a streight forward way to do it, but you might look into 'implicit indexing' as a short way of writing/accessing elements or sublists from a deeply complex nested list structure like returned from 'xml-parse':



(xml-type-tags nil nil nil nil)
 (set 'page (xml-parse (read-file "myfile.xml") (+ 1 4 8 16)))

(set 'sheet-type (page 0 2 4 1 0))


You may also want to look into 'ref' as a means to get the right indices of an element or sublist in the nested list returned:

>  (set 'L '(foo bar (sheet-name "my Sheet")))
(foo bar (sheet-name "my Sheet"))
> (ref 'sheet-name L)
(2 0)
> (L ((ref 'sheet-name L) 1))
foo
> (L ((ref 'sheet-name L) 0))
(sheet-name "my Sheet")

> ((L ((ref 'sheet-name L) 0)) 1)
"my Sheet"
>


This combines 'ref' and implicit indexing. Of course you could also use 'nth', 'first', 'last' etc., but it is more to write (and sometimes more readable too).



Lutz

Lutz

#4
.. and her is another trick:



;; you somehow extracted from a complex XML structure:

> sheet-spec
(sheet-name "MySheet")

;; define a procedure for this an other tags

 > (define (sheet-name s) (append "the sheet-name is: " s))

;; then evaluate the list structure

> (eval sheet-spec)

"the sheet-name is: MySheet"


This technique could also be used to generate XML/HTML from a list stucture.



Lutz

Dmi

#5
Hmm... nice trick! I sould to think around it...

Thanks!
WBR, Dmi