Lt Dan's legs Posted February 2, 2011 Posted February 2, 2011 I have a list like this (1 "A" "B" "C" 2 "A" "B") How would I go about getting ((1 . ("A" "B" "C"))(2 . ("A" "B"))) ? Quote
alanjt Posted February 2, 2011 Posted February 2, 2011 Quick and dirty... (defun test (lst / temp newlist) (foreach x (reverse lst) (if (numberp x) (setq temp (not (setq newlist (cons (list x temp) newlist)))) (setq temp (cons x temp)) ) ) newlist ) Quote
Lt Dan's legs Posted February 2, 2011 Author Posted February 2, 2011 Thanks! I was working on something similar but mine never worked out. Quote
alanjt Posted February 2, 2011 Posted February 2, 2011 Thanks! I was working on something similar but mine never worked out. Any time. Like I said' date=' it was [very'] quick and dirty. Quote
pBe Posted February 5, 2011 Posted February 5, 2011 I have a list like this(1 "A" "B" "C" 2 "A" "B") How would I go about getting ((1 . ("A" "B" "C"))(2 .("A" "B"))) ? Here's my attempt (defun test (ls / a b c) (foreach j ls (if (numberp j) (if a (setq c (cons (vl-list* (reverse b) a) c) a j b nil) (setq a j b nil)) (setq b (cons j b)) ) ) (setq newllist (reverse (cons (vl-list* (reverse b) a) c))) ) (test '(1 "A" "B" "C" 2 "A" "B")) ((("A" "B" "C") . 1) (("A" "B") . 2)) But for the love of me, i cant revese/construct the dotted pair to match the result Lt. Dan's legs request ((1 . ("A" "B" "C"))(2 .("A" "B"))) Alanjt I would have done it similar to your code but yours ended up with a non dotted pair.. any thoughts? glad the forum is up and running again Quote
MSasu Posted February 7, 2011 Posted February 7, 2011 But for the love of me, i cant revese/construct the dotted pair to match the result Lt. Dan's legs request ((1 . ("A" "B" "C"))(2 .("A" "B"))) Please don't forget that the second item of a dotted pair should be an atom, so the OP's request is a construction that isn't supported in AutoLISP. Regards, Mircea Quote
alanjt Posted February 7, 2011 Posted February 7, 2011 Please don't forget that the second item of a dotted pair should be an atom, so the OP's request is a construction that isn't supported in AutoLISP. Regards, Mircea And there's no point. You can still assoc a non-dotted pair list. eg. (using my sub) Command: (assoc 2 (test '(1 "A" "B" "C" "D" 2 "D" "F" "R" 5 "D" "E" "T"))) (2 ("D" "F" "R")) Quote
MSasu Posted February 7, 2011 Posted February 7, 2011 And there's no point. You can still assoc a non-dotted pair list. eg. (using my sub) Command: (assoc 2 (test '(1 "A" "B" "C" "D" 2 "D" "F" "R" 5 "D" "E" "T"))) (2 ("D" "F" "R")) I’m afraid it has to do with the consistency of accessing data stored in key & value style while still preserving the data type of the "value" (see associated lists): (cdr '(2 . 1)) will return 1 (cdr '(2 1 2 3)) will return '(1 2 3) (cdr '(2 (1 2 3))) will return '((1 2 3)) Regards, Mircea Quote
alanjt Posted February 7, 2011 Posted February 7, 2011 I’m afraid it has to do with the consistency of accessing data stored in key & value style while still preserving the data type of the "value" (see associated lists): (cdr '(2 . 1)) will return 1 (cdr '(2 1 2 3)) will return '(1 2 3) (cdr '(2 (1 2 3))) will return '((1 2 3)) Regards, Mircea Just use cadr instead of cdr to retrieve the sublist. 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.