Jump to content

Add itens to a Sublist


rog1n

Recommended Posts

Hello, I have a list with sublist, how I add a new item to a sublist if the first element is equal to a variable:
Ex:
lst = (("2" "b") ("1" "a"))

x="2"

y="c"

result: (("2" "b" "c") ("1" "a"))

 

but if the variable not exist on the list will create a new sublist like:
 lst = (("2" "b") ("1" "a"))

x="3"

y="c"

result: (("2" "b" ) ("1" "a")("3" "c"))

 

below a code I trying to do this...

(setq found nil index 0)
   (if lst
    (progn
      (While (and (setq f (nth index lst)) (not found))
        (if (= (car f) x)
          (progn
              (setq found t)
	     ;I dont know how add the value to sublist I think is simple with mapcar 
          )
        )
        (setq index (1+ index))
      )
      (if (not found) (setq lst (cons (cons x (list y)) lst)))  
    )
    (setq lst (list (list x y)))
      )
    )

 

Link to comment
Share on other sites

3 hours ago, rog1n said:

Hello, I have a list with sublist, how I add a new item to a sublist if the first element is equal to a variable:
....

 

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

  • Thanks 1
Link to comment
Share on other sites

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"))

 

  • Like 1
  • Agree 1
  • Thanks 1
Link to comment
Share on other sites

22 hours ago, Lee Mac said:

assoc & subst are the key functions here -...

For example - say you start with the following list:ist:...

.. Putting this all together with an if statement to branch accordingly, we can write a function such as the following:

 

Pandemic or not Lee, you always have time to write a thorough explanation of things 👍

🙂

 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...