Jump to content

Recommended Posts

Posted

Hello.

 

I'm pulling my hair out trying figure out how to use associative lists.

 

My goal is to create a function that would populate a global list. It need to accept a name for assoc and value that it will append to it.

For example an existing list:

("blah1" (1 2 3 5)
"blah2" (4 3 2 1)
)

I'd like to add (4 6 7) to "blah1" list and (1 5) to "blah2" also add another list as "blah3" with values (3 4 5)

So the end result would be:

("blah1" (1 2 3 5 [color=red]4 6 7[/color])
"blah2" (4 3 2 1 [color=red]1 5[/color])
"blah3" ([color=red]3 4 5[/color])
)

In javascript this is done ridiculously simple:

var myList = {
   blah1: [1, 2, 3, 5],
 blah2: [4, 3, 2, 1]
};
function addToList (key val)
{
   myList[key].push(val);
}

addToList("blah1", [4, 6, 7]);
addToList("blah2", [1,5]);
addToList("blah3", [3, 4, 5]);

 

Can someone please help.

 

Thank you.

Posted

Are you wanting to be prompted for the new/additional list values or are there any other source for the those?

Posted

No, no prompts, other part of the script will supply them, I need a function that would populate the list with supplied values

Posted (edited)

(defun addTListDemo  (addvalues existinglist)
     (foreach
            value  addvalues
           (Setq existinglist
                      (if (setq AddorNew
                                     (assoc (car value)
                                            existinglist))
                            (subst (list (car AddorNew)
                                         (append (cadr AddorNew)
                                                 (cadr value)))
                                   AddorNew
                                   existinglist)
                            (append existinglist (list value))
                            )
                 )
           )
     existinglist
     )

 

(setq lst '(( "BLAH1" (1 2 3 5))("BLAH2" (4 3 2 1))))

( addtlistdemo '(("BLAH1" (4 6 7))("BLAH2" (1 5))("BLAH3" (3 4 5))) lst)

(("BLAH1" (1 2 3 5 [b][color="blue"]4 6 7[/color][/b])) ("BLAH2" (4 3 2 1 [b][color="blue"]1 5[/color][/b])) [b][color="blue"]("BLAH3" (3 4 5))[/color][/b])

 

INDIVIDUAL

 

(defun addTListDemoi  (addvalue existinglist)                  
     (if (setq AddorNew (assoc (car addvalue) existinglist))
         	(subst (list (car AddorNew)
                      (append (cadr AddorNew)(cadr addvalue)))
                      AddorNew existinglist)
         	(append existinglist (list addvalue))
         )
     )

 

(addtlistdemoi '[b][color="blue"]("BLAH2" (1 5))[/color][/b] lst)
(("BLAH1" (1 2 3 5)) ("BLAH2" (4 3 2 1[b][color="blue"] 1 5[/color][/b])))

(addtlistdemoi '[b][color="blue"]("BLAH3" (3 4 5))[/color][/b] lst)
(("BLAH1" (1 2 3 5)) ("BLAH2" (4 3 2 1)) [b][color="blue"]("BLAH3" (3 4 5))[/color][/b])

 

 

 

OFF TOPIC @kaan27: exceeded quota for PM / Who is Original Author/ Cant see the image on the link / What does it do / Why not post the request on the board?

Edited by pBe
Posted

Thank you very much! The second example is exactly what I was looking for.

Posted

Good for you vanowm and you are welcome. hope you'll learn from the demo code.

 

Cheers :)

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...