Inserting into a nested list (with assoc)

Started by kanen, July 28, 2013, 10:26:34 PM

Previous topic - Next topic

kanen

I have a list:


(set (global 'me) '(
  (80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))) )
  (25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))) )
  ))


I can get to it fairly easily;


(assoc 80 me)                     ; returns all "80" results
(assoc (list 80 1010) me)         ; returns all "80" + "1010" results
(last (assoc (list 80 1010) me))  ; returns the list for 80 + 1010


My question is, how do I insert a list into the results from (last (assoc (list 80 1010) me)) easily and quickly? I can get the results, insert into them, then re-insert the new list... but, I'd rather do this all-at-once.
. Kanen Flowers http://kanen.me[/url] .

cormullion

#1
Perhaps set-ref can help:


(set-ref (assoc (list 80 1010) me) me (append $it (list "more stuff")))

(
 (80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))
   "more stuff"))
 (25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))
 ))

kanen

#2
Thanks!



I've never even used (set-ref) before, in all my years of newLisp. Everyday, as they say... you learn a thing.
. Kanen Flowers http://kanen.me[/url] .

cormullion

#3
set-ref is nearly as awesome as set-ref-all with match wild-cards! :)

Lutz

#4
This can be made even simpler. The example searches me twice. First assoc searches in me then set-ref does search in me again for the expression found by assoc.



In this case you could just use setf:


(setf (assoc '(80 1010) me)  (append $it (list "more stuff")))

me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))
   "more stuff"))
 (25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))


The place reference returned by assoc can be used by setf



.. and you can make it even shorter using push:


(push "more stuff" (assoc '(80 1010) me) -1)

me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))
   "more stuff"))
 (25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))


push also can use place references and you can better control where to exactly put the new piece:


(push "more stuff" (assoc '(80 1010) me) -1 -1)

me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)
    "more stuff")))
 (25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))

 

in this case, the new element is pushed one nesting level higher.