vanowm Posted April 5, 2015 Posted April 5, 2015 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. Quote
pBe Posted April 5, 2015 Posted April 5, 2015 Are you wanting to be prompted for the new/additional list values or are there any other source for the those? Quote
vanowm Posted April 5, 2015 Author Posted April 5, 2015 No, no prompts, other part of the script will supply them, I need a function that would populate the list with supplied values Quote
pBe Posted April 5, 2015 Posted April 5, 2015 (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 April 5, 2015 by pBe Quote
vanowm Posted April 5, 2015 Author Posted April 5, 2015 Thank you very much! The second example is exactly what I was looking for. Quote
pBe Posted April 5, 2015 Posted April 5, 2015 Good for you vanowm and you are welcome. hope you'll learn from the demo code. Cheers Quote
Recommended Posts
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.