TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: daron on July 12, 2005, 01:32:28 PM
-
Take this list:
(setq stdWallList '((0 . "Basement") (1 . "Garage") (2 . "Stoop")
(3 . "Arm") (4 . "Haunch")
)
)
and find the associated value of "Stoop".
What I'm looking for is the reverse of:
(cdr (assoc 2 stdWallList)
-
(cadr (assoc "Stoop" (mapcar '(lambda (x) (list (cdr x)(car x))) stdWallList)))
edit - changed to cadr
-
That's good CAB. I came up with:
(defun revassoc (itempred itemcomp / newitemlist)
(foreach item itempred
(if (= itemcomp (cdr item))
(setq newitemlist (append newitemlist (list item)))
)
)
newitemlist
)
Not too challenging. I'd like to see the varied possibilities.
-
(defun test (str lst)
(caar (vl-remove-if-not '(lambda (x) (eq (cdr x) str)) lst))
)
-
Thanks,
I changed mine to cadr ILO cdr
-
With a small edit, the one I made above can be made to reflect exactly the way assoc does when it encounters multiple key values:
;example: (revassoc stdwalllist "Garage")
(defun revassoc (itempred itemcomp / newitemlist)
(foreach item itempred
(if (= itemcomp (cdr item))
(setq newitemlist (append newitemlist (list item)))
)
)
(car newitemlist)
)
My previous one returns a list of all key's equal. This one returns the first of all in just a list, instead of a nested list.
-
Here is another.
(defun revassoc (itm lst)
(vl-some '(lambda (x) (if (= (cdr x) itm)(car x))) lst)
)
Did you want the lisy back like this?
(defun revassoc (itm lst)
(vl-some '(lambda (x) (if (= (cdr x) itm) x)) lst)
)
-
(cadr (assoc "Stoop" (mapcar '(lambda (x) (list (cdr x)(car x))) stdWallList)))
edit - changed to cadr
I would keep the use of (cdr) since you are supposed to be using an association list. Which means you need to build the association list in reverse....just a slight mod. to CAB's code:
(cdr (assoc "Stoop" (mapcar '(lambda (x) (cons (cdr x)(car x))) stdWallList)))
:D
-
What we have here is a failure to communicate.
:shock:
What I'm looking for is the reverse of:
(cdr (assoc 2 stdWallList))
That doesn't return a list. :D :D
-
That doesn't return a list. :D :D
Nor does the code I modified with (cons) in place of (list) 8) :P
-
Oh, I see what you did. :oops: The internal result is a dotted pair.
That way it has the same look as the usual code.
How about this:
(defun rv_list (lst)
(mapcar '(lambda (x) (cons (cdr x)(car x))) lst)
)
(cdr (assoc "Stoop" (rv_list stdWallList)))
-
Exactly! I have one of these for switching around layer names for a client with a strange layer naming "standard". The layers are in an assoc list ("MyLayer" . "HisLayer"), etc., then when I want to flip to my layers I just call it (fliplay laylist nil) or to his (fliplay laylist t)....depending the arg. being true I just flip 'em around.
-
Sorry if there was any miscom. I was trying to leave it a little open. It's good for variations that don't get too far away from the point. It could spur ideas that weren't there to begin with. I should've left out the cdr assoc bit.
-
(defun rvAssoc (item lst)
(cond ((null lst) nil)
((eq (cdar lst) item) (car lst))
((rvAssoc item (cdr lst)))
)
)
-
Sweet recursion. I had hoped for something like that too. Thanks Stig.