Page 1 of 1
Inserting into a nested list (with assoc)
Posted: Mon Jul 29, 2013 5:26 am
by kanen
I have a list:
Code: Select all
(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;
Code: Select all
(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.
Re: Inserting into a nested list (with assoc)
Posted: Mon Jul 29, 2013 7:45 am
by cormullion
Perhaps
set-ref can help:
Code: Select all
(set-ref (assoc (list 80 1010) me) me (append $it (list "more stuff")))
Code: Select all
(
(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)))
))
Re: Inserting into a nested list (with assoc)
Posted: Mon Jul 29, 2013 3:25 pm
by kanen
Thanks!
I've never even used (set-ref) before, in all my years of newLisp. Everyday, as they say... you learn a thing.
Re: Inserting into a nested list (with assoc)
Posted: Mon Jul 29, 2013 5:46 pm
by cormullion
set-ref is nearly as awesome as set-ref-all with match wild-cards! :)
Re: Inserting into a nested list (with assoc)
Posted: Tue Jul 30, 2013 1:42 pm
by Lutz
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:
Code: Select all
(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:
Code: Select all
(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:
Code: Select all
(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.