TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: daron on July 12, 2005, 01:32:28 PM

Title: (challenge)? ASSOCiate by value vs. key
Post by: daron on July 12, 2005, 01:32:28 PM
Take this list:
Code: [Select]
(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:
Code: [Select]
(cdr (assoc 2 stdWallList)
Title: (challenge)? ASSOCiate by value vs. key
Post by: CAB on July 12, 2005, 02:00:21 PM
Code: [Select]
(cadr (assoc "Stoop" (mapcar '(lambda (x) (list (cdr x)(car x))) stdWallList)))
edit - changed to cadr
Title: (challenge)? ASSOCiate by value vs. key
Post by: daron on July 12, 2005, 02:04:52 PM
That's good CAB. I came up with:
Code: [Select]
(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.
Title: (challenge)? ASSOCiate by value vs. key
Post by: whdjr on July 12, 2005, 02:18:47 PM
Code: [Select]
(defun test (str lst)
  (caar (vl-remove-if-not '(lambda (x) (eq (cdr x) str)) lst))
)
Title: (challenge)? ASSOCiate by value vs. key
Post by: CAB on July 12, 2005, 02:20:09 PM
Thanks,
I changed mine to cadr ILO cdr
Title: (challenge)? ASSOCiate by value vs. key
Post by: daron on July 12, 2005, 02:23:20 PM
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:
Code: [Select]
;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.
Title: (challenge)? ASSOCiate by value vs. key
Post by: CAB on July 12, 2005, 02:37:39 PM
Here is another.
Code: [Select]
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cdr x) itm)(car x))) lst)
)


Did you want the lisy back like this?
Code: [Select]
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cdr x) itm) x)) lst)
)
Title: (challenge)? ASSOCiate by value vs. key
Post by: Jeff_M on July 12, 2005, 02:53:28 PM
Quote from: CAB
Code: [Select]
(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:
Code: [Select]
(cdr (assoc "Stoop" (mapcar '(lambda (x) (cons (cdr x)(car x))) stdWallList)))  :D
Title: (challenge)? ASSOCiate by value vs. key
Post by: CAB on July 12, 2005, 03:05:49 PM
Quote from: Coolhand Luke
What we have here is a failure to communicate.
:shock:
Quote from: daron
What I'm looking for is the reverse of:
Code: [Select]
(cdr (assoc 2 stdWallList))

That doesn't return a list. :D  :D
Title: (challenge)? ASSOCiate by value vs. key
Post by: Jeff_M on July 12, 2005, 03:07:53 PM
Quote from: CAB
That doesn't return a list. :D  :D
Nor does the code I modified with (cons) in place of (list)  8)  :P
Title: (challenge)? ASSOCiate by value vs. key
Post by: CAB on July 12, 2005, 03:22:11 PM
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:
Code: [Select]
(defun rv_list (lst)
  (mapcar '(lambda (x) (cons (cdr x)(car x))) lst)
)


(cdr (assoc "Stoop" (rv_list stdWallList)))
Title: (challenge)? ASSOCiate by value vs. key
Post by: Jeff_M on July 12, 2005, 03:26:55 PM
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.
Title: (challenge)? ASSOCiate by value vs. key
Post by: daron on July 12, 2005, 04:08:22 PM
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.
Title: (challenge)? ASSOCiate by value vs. key
Post by: SMadsen on July 12, 2005, 04:38:51 PM
Code: [Select]
(defun rvAssoc (item lst)
  (cond ((null lst) nil)
        ((eq (cdar lst) item) (car lst))
        ((rvAssoc item (cdr lst)))
  )
)
Title: (challenge)? ASSOCiate by value vs. key
Post by: daron on July 13, 2005, 12:50:20 PM
Sweet recursion. I had hoped for something like that too. Thanks Stig.