Recursive construction of associated list

Recursive construction of associated list

giuseppebeatrice222
Contributor Contributor
1,160 Views
4 Replies
Message 1 of 5

Recursive construction of associated list

giuseppebeatrice222
Contributor
Contributor

I read a very interesting forum in https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/recursive-assoc-and-subst/m-p/782358...

where Gile gave an optimum explanation and a solution to a question regarding the search of associated values and the substitution of values in nested  association lists.

Now I have a subsequente question, of how making from the scratch a nested association list, giving a key list and the final, nested value.

For example, if I want to assign to the variable al a nested association list  (setq al '(("GRP1" ("A" (2 (21 . "b")))))) is it possible to construct a recursive function of the tipe (MAKEORSUBST (list "GRP1" "A" 2) '(21 . "b"))

In addition, if I have fixed the preceding value for al and I want to add a new value (22 . "c") to the inner value (21 . "b") with the last key 2, can I obtain the result with a command of type (MAKEORSUBST (list "GRP1" "A" 2) '(22 . "c"))?

I have made numerous attempts to construct such a function on the basis of the functions provided by Gile in that forum, but with no results.

Please, can Gile himself or someone other give me a help?

Thanks in advance

0 Likes
Accepted solutions (1)
1,161 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

OT. If you want to draw someone's attention you can use the @ character to do that: @_gile 

Then he receives an email that he was mentioned by someone.

0 Likes
Message 3 of 5

ronjonp
Mentor
Mentor

HERE is some code by master ElpanovEvgeniy.

Another thread HERE to flatten lists by master MP 🙂

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

The makeorsubst function also need an argument for the exisitng list/tree (nil if none)

(defun makeorsubst (keys val lst / sub)
  (cond
    ((null keys)
     (if (setq sub (assoc (car val) lst))
       (subst val sub lst)
       (cons val lst)
     )
    )
    ((setq sub (assoc (car keys) lst))
     (subst (cons (car keys)
		  (makeorsubst (cdr keys) val (cdr sub))
	    )
	    sub
	    lst
     )
    )
    (T
     (append
       (list (cons (car keys) (makeorsubst (cdr keys) val nil)))
       lst
       )
    )
  )
)
_$ (setq tree (makeorsubst '("GRP1" "A" 2) '(21 . "b") nil))
(("GRP1" ("A" (2 (21 . "b")))))
_$ (setq tree (makeorsubst '("GRP1" "A" 2) '(22 . "c") tree))
(("GRP1" ("A" (2 (22 . "c") (21 . "b")))))
_$ (setq tree (makeorsubst '("GRP1" "A" 2) '(22 . "d") tree))
(("GRP1" ("A" (2 (22 . "d") (21 . "b")))))
_$ (setq tree (makeorsubst '("GRP1" "A" 3) '(31 . "a") tree))
(("GRP1" ("A" (3 (31 . "a")) (2 (22 . "d") (21 . "b")))))


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

giuseppebeatrice222
Contributor
Contributor

Many many thanks Gile, it works very fine.

Your knowledge of recursion is fantastic.

0 Likes