Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 06/21/2020 in all areas

  1. assoc & subst are the key functions here - similar to how you might manipulate the DXF data returned by entget. You are working with an association list wherein the first element of each sublist represents a key, and the tail of the list represents the data associated with that key. You can use assoc to test whether a sublist with a given key exists, and then use subst to substitute the sublist with a modified version within the main list. For example - say you start with the following list: _$ (setq lst '(("2" "b") ("1" "a"))) (("2" "b") ("1" "a")) And you have the following new values: _$ (setq key "2" val "c") You can test whether the key exists using assoc: _$ (setq old (assoc key lst)) ("2" "b") Since it does, you can modify it using the value returned by assoc: _$ (setq new (append old (list val))) ("2" "b" "c") And finally, you can substitute it into the main list using subst: _$ (setq lst (subst new old lst)) (("2" "b" "c") ("1" "a")) If the key does not exist, you can use cons to push it onto the list: _$ (setq key "3" val "c") _$ (assoc key lst) nil _$ (setq lst (cons (list key val) lst)) (("3" "c") ("2" "b" "c") ("1" "a")) Putting this all together with an if statement to branch accordingly, we can write a function such as the following: (defun addtolist ( key val lst / old ) (if (setq old (assoc key lst)) (subst (append old (list val)) old lst) (cons (list key val) lst) ) ) _$ (setq lst '(("2" "b") ("1" "a"))) (("2" "b") ("1" "a")) _$ (setq lst (addtolist "2" "c" lst)) (("2" "b" "c") ("1" "a")) _$ (setq lst (addtolist "3" "d" lst)) (("3" "d") ("2" "b" "c") ("1" "a"))
    2 points
  2. Pandemic or not Lee, you always have time to write a thorough explanation of things
    1 point
  3. sample source (setq ls '(("2" "b") ("1" "a")("2" "c")("3" "c")("1" "c")("2" "a"))) ( (lambda (lst / x y) (foreach itm ls (setq x (Car itm)) (setq y (cadr itm)) (princ (Strcat "\nX = " x )) (princ (Strcat "\nY = " y )) (princ "\n========" ) ;;; This section ;;; (setq lst (if (setq f (assoc x lst)) (subst (cons x (cons y (cdr f))) f lst) (cons (list x y) lst) ) ) ;;; This section ;;; ) ) nil ) X = 2 Y = b ======== X = 1 Y = a ======== X = 2 Y = c ======== X = 3 Y = c ======== X = 1 Y = c ======== X = 2 Y = a ======== (("3" "c") ("1" "c" "a") ("2" "a" "c" "b")) <-- end result You may have to do some kind of sorting either on the end result or on the source. HTH
    1 point
×
×
  • Create New...