Page 1 of 1

Selecting part of nested list?

Posted: Mon May 03, 2010 5:25 pm
by cormullion
If i have a nested list, is it easy or possible to select a slice of it?

For example, say I find the slice I want using ref and store the start and end points in symbols:

Code: Select all

(println start-pos)
;-> ((0 3 1072))
(println end-pos)
;-> ((0 3 4973))
How can I remove everything outside that range? Or is it impossible?

Re: Selecting part of nested list?

Posted: Thu May 06, 2010 6:34 pm
by cormullion
I resorted to doing it the old-fashioned way... :)

Code: Select all

(set 'start-pos (ref  '(h2 "4. Functions in alphabetical order") xml-source))
(set 'end-pos (ref  '(a ((name "appendix") (id "appendix")))  xml-source ))

(for (x (last start-pos) (last end-pos))
   (push (xml-source (extend (chop start-pos) (list x))) res -1))

Re: Selecting part of nested list?

Posted: Thu May 06, 2010 11:58 pm
by Lutz
Ahh, now I understand, what you were looking for:

Code: Select all

start-pos   ;=>  (0 3 1072)
end-pos   ;=> (0 3 4973)

(set 'start (last start-pos))
(set 'size (- (last end-pos) (last start-pos)))

(start size (xml-source (chop start-pos)))
We assume that the chopped part of start-pos and end-pos are the same index vector. From you first post in the thread I assume start-pos an end-pos are both 3-element-lists where the first two elements are the same. So basically (xml-source (chop start-pos)) picks the sublist from which we take a slice of.

Re: Selecting part of nested list?

Posted: Fri May 07, 2010 5:11 pm
by cormullion
Yes, that's much better!