Selecting part of nested list?

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Selecting part of nested list?

Post 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?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Selecting part of nested list?

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

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

Re: Selecting part of nested list?

Post 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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Selecting part of nested list?

Post by cormullion »

Yes, that's much better!

Locked