list inside list

list inside list

nitin.rathod3CW9S
Contributor Contributor
719 Views
5 Replies
Message 1 of 6

list inside list

nitin.rathod3CW9S
Contributor
Contributor
I was wondering if you can help me with the list updating issue I have.
 
Here is the issue, I have a list of four values that look like below.
[0] ("list1" ( "1"  "2" ))
[1] ("list2" ( "11"  "12" ))
[2] ("list3" ( "21"  "22" ))
[3] ("list4" ( "31"  "32" ))
 
I want to add the value "13" to "List2" so that the output looks like the list below.
[0] ("list1" ( "1"  "2" ))
[1] ("list2" ( "11"  "12" "13"))
[2] ("list3" ( "21"  "22" ))
[3] ("list4" ( "31"  "32" ))
 
If the input is to add "41" to "list5" then the output should look like the list below.
[0] ("list1" ( "1"  "2" ))
[1] ("list2" ( "11"  "12" ))
[2] ("list3" ( "21"  "22" ))
[3] ("list4" ( "31"  "32" ))
[4] ("list5" ( "41" ))
 
Let me know if you can help me with a sample code to do something like this.
0 Likes
Accepted solutions (1)
720 Views
5 Replies
Replies (5)
Message 2 of 6

pbejse
Mentor
Mentor
Accepted solution

@nitin.rathod3CW9S wrote:
 
Let me know if you can help me with a sample code to do something like this.

Sample code

(setq MainList
       '(("list1" ("1" "2"))
	 ("list2" ("11" "12"))
	 ("list3" ("21" "22"))
	 ("list4" ("31" "32"))
	)
)

(Defun _BuildAndEdit (key val lst)
  (if (setq f (assoc key lst))
    (subst (list key (append (cadr f) (list val))) f lst)
    (append lst (list (list key (list val))))
  )
)

 

_$ MainList
(("list1" ("1" "2")) ("list2" ("11" "12")) ("list3" ("21" "22")) ("list4" ("31" "32")))

_$ (setq MainList (_BuildAndEdit "list2" "13"  MainList))
(("list1" ("1" "2")) ("list2" ("11" "12" "13")) ("list3" ("21" "22")) ("list4" ("31" "32")))

_$ (setq MainList (_BuildAndEdit "list5" "41"  MainList))
(("list1" ("1" "2")) ("list2" ("11" "12" "13")) ("list3" ("21" "22")) ("list4" ("31" "32")) ("list5" ("41")))

 HTH

 

0 Likes
Message 3 of 6

Kent1Cooper
Consultant
Consultant

What are the bracketed numbers at the start of the lines?  Just for reference?  Are they variable names?  (You can use  [0]  as a variable name, but not just  0 .)

 

Your wording "a list of four values" suggests that the actual overall list is like this:

(("list1" ( "1"  "2" )) ("list2" ( "11"  "12" )) ("list3" ( "21"  "22" )) ("list4" ( "31"  "32" )))

in which case the bracketed numbers would be the index numbers of the "values" [i.e. sub-lists] in the overall list.  Is that right?  Is that overall list stored under a variable name?

Is [for example]  ("list1" ( "1"  "2" ))  really a list that includes a text string "list1" as its first item, along with a nested list ( "1"  "2" ) as its second item?  If the overall list above is not right, is this one stored in a variable called  [0]  ?  Your wording  ' I want to add the value "13" to "List2" '  makes it sound like maybe  list1  [without the quotation marks] is a variable name [not a text string] that contains the list  ( "1"  "2" )  as its only item.  Is that the situation?

Kent Cooper, AIA
0 Likes
Message 4 of 6

nitin.rathod3CW9S
Contributor
Contributor

thanks a lot @pbejse . that was accurate solution I was looking for.

Moreover thanks for such a quick response. thanks again.

0 Likes
Message 5 of 6

nitin.rathod3CW9S
Contributor
Contributor

[0], [1], [2],... represents index of the list inside list. actual list will look like below as you guessed already.

 

(("list1" ( "1"  "2" )) ("list2" ( "11"  "12" )) ("list3" ( "21"  "22" )) ("list4" ( "31"  "32" )))

 

thanks for your response @Kent1Cooper 

0 Likes
Message 6 of 6

pbejse
Mentor
Mentor

@nitin.rathod3CW9S wrote:

thanks a lot @pbejse . that was accurate solution I was looking for.

Moreover thanks for such a quick response. thanks again.


Good for you, That's just one way of getting the result your looking for. its a Q&D code, you can develop it more by including conditions for sorting and some.

 

You are welcome @nitin.rathod3CW9S 

 

 

0 Likes