destructive append to a string

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

destructive append to a string

Post by Dmi »

Is there a way to destructive append to the end of string?
Similar to 'push?
WBR, Dmi

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

Post by HPW »

Use:

From the help-file:

Code: Select all

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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Oh! Thanks!
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... 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':

Code: Select all

(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:

Code: Select all

>  (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

.. and her is another trick:

Code: Select all

;; 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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Hmm... nice trick! I sould to think around it...
Thanks!
WBR, Dmi

Locked