Selecting part of nested list?

Started by cormullion, May 03, 2010, 10:25:33 AM

Previous topic - Next topic

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:


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


How can I remove everything outside that range? Or is it impossible?

cormullion

#1
I resorted to doing it the old-fashioned way... :)


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

Lutz

#2
Ahh, now I understand, what you were looking for:



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.

cormullion

#3
Yes, that's much better!