Grrr 发表于 2022-7-5 17:46:30

Assoc list - questions

Hi guys,
I was practicing with lists and I have few questions regarding association lists.
So what is the definition of association list?
As far I understand is that each item from the list has its 2 associations (with dotted pair), Example:

        (setq mylistA        '(                ("TAG1" . "VALUE1A")                ("TAG2" . "VALUE2A")                ("TAG3" . "VALUE3A")                ("TAG4" . "VALUE4A")                ("TAG5" . "VALUE5A")                ("TAG6" . "VALUE6A")        ))
But is it possible to create more (than 2) associations for an item like this:

("TAG1" "VALUE1A" "XVALUE1A")
Or dotted:

("TAG1" . "VALUE1A" . "XVALUE1A")
With cons function I could achieve this:

(("TAG1" . "VALUE1A") . "XVALUE1A")
I didn't find anything about manipulating association lists - to see what forms it could have (only 2 associations per item or more? /dotted or not?/ ).
The only example for assoc list I could have is with (entget (car (entsel))).
 
The real question is for their usage, I mean is it possible to display items associated with their "tagname", but work with their "associated value", for example this attempt:

;Match portion(items) from list A to list B(defun C:test (/ mylistA chosenlst mylistB)(setq mylistA        '(                ("TAG1" . "VALUE1A")                ("TAG2" . "VALUE2A")                ("TAG3" . "VALUE3A")                ("TAG4" . "VALUE4A")                ("TAG5" . "VALUE5A")                ("TAG6" . "VALUE6A")        ))(setq chosenlst (LM:FiltListBox "Select items" (mapcar 'car mylistA) t))(setq mylistB        '(                ("TAG1" . "VALUE1B")                ("TAG2" . "VALUE2B")                ("TAG3" . "VALUE3B")                ("TAG4" . "VALUE4B")                ("TAG5" . "VALUE5B")                ("TAG6" . "VALUE6B")        ))(princ "\n**************************************************************************\n")(LM:princl mylistA f)(princ "\n**************************************************************************\n")(LM:princl mylistB f)(princ))
Where I try to apply the values from mylistA to mylistB, from the chosen TAG associations.(its not done because I don't know how)

ymg3 发表于 2022-7-5 18:09:13

grrr,
 
Association list are not necessarily dotted.
 
You can do :
 

(setq mylistA       '(("TAG1""VALUE1A" "VALUE1B" "VALUE1C" "VALUE1D")         ("TAG2""VALUE2A" "VALUE2B" "VALUE2C" "VALUE2D")("TAG3""VALUE3A" "VALUE3B" "VALUE3C" "VALUE3D")         ("TAG4""VALUE4A" "VALUE4B" "VALUE4C" "VALUE4D")("TAG5""VALUE5A" "VALUE5B" "VALUE5C" "VALUE5D")("TAG6""VALUE6A" "VALUE6B" "VALUE6C" "VALUE6D") ))(("TAG1" "VALUE1A" "VALUE1B" "VALUE1C" "VALUE1D") ("TAG2" "VALUE2A" "VALUE2B" "VALUE2C" "VALUE2D") ("TAG3" "VALUE3A" "VALUE3B" "VALUE3C" "VALUE3D") ("TAG4" "VALUE4A" "VALUE4B" "VALUE4C" "VALUE4D") ("TAG5" "VALUE5A" "VALUE5B" "VALUE5C" "VALUE5D") ("TAG6" "VALUE6A" "VALUE6B" "VALUE6C" "VALUE6D"))_$ (assoc "TAG3" MYLISTA)("TAG3" "VALUE3A" "VALUE3B" "VALUE3C" "VALUE3D")_$ (nth 3 (assoc "TAG3" mylista))"VALUE3C"_$(cdr (assoc "TAG4" mylista))("VALUE4A" "VALUE4B" "VALUE4C" "VALUE4D")
 
The only restriction is that you search on the first item of each sublist
in the association list.
 
With dotted pair as the name implies you are restricted to 2 items

Tharwat 发表于 2022-7-5 18:19:06

Hi,
 
The first element of any list is considered the KEY that you can find your desired list with, and you can not have two dotted pairs like ("A" . "B" . "C") and that can not be found by Assoc function. But to have these fore-said example in one list without dots it should found the list if the KEY is found of course.
 
READ THIS

Lee Mac 发表于 2022-7-5 18:24:07

You may wish to read this section of my tutorial on Building Association Lists.
 
An important point to note is that dotted pairs are very different to standard lists (linked lists) in AutoLISP in the way in which they are stored in memory. Where every item in a linked list occupies two memory registers (one allocated to the item value, the other storing a pointer to the memory address of the next list item), the two data items represented by a dotted pair are stored in two adjacent memory registers, which is why the (cdr) of a dotted pair yields the associated value, and not the remaining list.

Grrr 发表于 2022-7-5 18:39:06

Thanks alot guys,
Still I wanted to ask:

(setq alist'(       ("Item 1" . "Value 1")       ("Item 2" . "Value 2")       ("Item 3" . "Value 3")   ))
Does each item also appears to be a list? I mean, is this a list also? :

      ("Item 2" . "Value 2")
The reason I'm asking is because I have constructed a custom association list like this:

((entname . ) (entitytype . LWPOLYLINE) (entitylayer . VLD_ТОПЛОИЗОЛАЦИЯ))((entname . ) (entitytype . INSERT) (entitylayer . VLD_ПРОЗОРЦИ) (blockname . VLD_Отвор-За-Дограма))
And I'm trying to display some of the associations in a listbox, and others to use within ssadd.
Still its a mind-blowing idea for me, since I except user to choose "memory registers for each list item" and use other "memory registers".
(Damn, its hard to describe what I'm trying to do - in simplier words: I'm trying to build a simple listbox filter routine)

Tharwat 发表于 2022-7-5 18:51:21

 
Yes it is.
Few tests for you if you would like:
 

_$ (setq alist '(("Item 1" . "Value 1")("Item 2" . "Value 2")("Item 3" . "Value 3")))(("Item 1" . "Value 1") ("Item 2" . "Value 2") ("Item 3" . "Value 3"))_$ (assoc "Item 2" alist)("Item 2" . "Value 2")_$ (assoc "Item 3" alist)("Item 3" . "Value 3")
页: [1]
查看完整版本: Assoc list - questions