Steven P Posted June 30, 2017 Posted June 30, 2017 Good afternoon and thanks in advance for any help you can give. I have a nested list, and want to extract the items in the nested part of the list, but am not sure how to do that. I don't suppose anyone could give me a pointer what to look for? For example I have the nested list called mylist: (List0 (item01a item01b item01c) List1 (Item1a Item1b Item1c) List2....) I can use the function (nth x mylist) where x is the list number (1 gives 'list1'), but how do I get items from that list? (say Item1a or item2c etc). Thanks again Quote
ronjonp Posted June 30, 2017 Posted June 30, 2017 They way you have your example list structured you can use (cadr (member: (cadr (member "list2" (list "list1" '("item1a" "item1b" "item1c") "list2" '("item2a" "item2b" "item2c") "list3" '("item3a" "item3b" "item3c") ) ) ) If it's structured more like this you can use (cdr (assoc: (cdr (assoc "list2" '(("list1" ("item1a" "item1b" "item1c")) ("list2" ("item2a" "item2b" "item2c")) ("list3" ("item3a" "item3b" "item3c")) ) ) ) Then you can use vl-some: (vl-some '(lambda (x) (if (= "item2b" x) x ) ) (cadr (member "list2" (list "list1" '("item1a" "item1b" "item1c") "list2" '("item2a" "item2b" "item2c") "list3" '("item3a" "item3b" "item3c") ) ) ) ) Quote
Steven P Posted June 30, 2017 Author Posted June 30, 2017 That doesn't seam to work for me, but I will look at the cadr command to see if I can work it out. In your examples above will that let you specify which item in the nested list you want returned? I can't see how it does it Quote
ronjonp Posted June 30, 2017 Posted June 30, 2017 What are the data types in your list? Perhaps post the actual list? Also be aware that if these are strings the functions are case sensitive. Quote
David Bethel Posted June 30, 2017 Posted June 30, 2017 You may want to look into (atom) function. -David Quote
Steven P Posted June 30, 2017 Author Posted June 30, 2017 hi, The items in the list are strings and are in this form: ((item1 (item1a item2a item3a item4a)) (item2 (item2a)) (item3 (item3a)) (item4 (item4a))(item5(item5a item5b item5c))) the earlier parts of the routine make the list up and this is how it shows up in the command line) so looking for nested list item x in list at position y. If I do (nth 2 mylist) it returns "item3" - the main list name. Great I need that later anyway. But sort of looking how to go down and choose item 1, 2, or 3 from that list. Sort of joining 2 nth commands if that makes sense. The CDR and CADR say they only return the 2nd item in the list? Or can I get the nested list separated and then scroll through it (the reason I am using nested lists is that I don't know how many lists there will be each time, otherwise I would have gone for a simple set of lists, populated each one and looked through them in a much easier way) Quote
ronjonp Posted June 30, 2017 Posted June 30, 2017 (cadr (member is what you need for your data structure .. if it's not too late, I'd restructure to be an association list. Here's a quick example using two index numbers .. although there's many ways to skin this cat (defun _getsomething (l idx1 idx2) (nth idx2 (cadr (member (nth idx1 l) l)))) (_getsomething (list "list1" '("item1a" "item1b" "item1c") "list2" '("item2a" "item2b" "item2c") "list3" '("item3a" "item3b" "item3c") ) 2 2 ) Quote
BIGAL Posted July 1, 2017 Posted July 1, 2017 (edited) Back to using nths you can have them multiple levels deep fairly straight forward here for your example (setq lst (list "list1" '("item1a" "item1b" "item1c") "list2" '("item2a" "item2b" "item2c") "list3" '("item3a" "item3b" "item3c"))) (setq num (/ (length lst) 2)) (setq y 0) ;2nd item (setq x 0) (repeat num (setq y 0) (setq valname (nth x lst)) (setq x (+ x 1)) (setq val1 (nth y (nth x lst))) (setq val2 (nth (setq y (+ y 1))(nth x lst))) (setq val3 (nth (setq y (+ y 1))(nth x lst))) (setq x (+ x 1)) (alert (strcat valname "\n" val1 "\n" val2 "\n" val3)) ) Edited July 1, 2017 by BIGAL Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.