Here is the code snippet from my program, I am trying to build a associative list from two regular lists in the while loop. Does ""append " or "cons" only work for simple lists?
; Given attributeNameList attributeValTypeList which are always of the same length
(setq listCntr 0)
(while (<= listCntr (length attributeNameList))
(setq attrTagTypeAssnList (append attrTagTypeAssnList (list ((nth listCntr attributeNameList) (nth listCntr attributeValTypeList))) ))
(setq listCntr (1+ listCntr))
)
How can I dynamically build the association list.Any Ideas?
Thanks
Insia
@Anonymous wrote:... I am trying to build a associative list from two regular lists in the while loop. Does ""append " or "cons" only work for simple lists?
; Given attributeNameList attributeValTypeList which are always of the same length
(setq listCntr 0)
(while (<= listCntr (length attributeNameList))
(setq attrTagTypeAssnList (append attrTagTypeAssnList (list ((nth listCntr attributeNameList) (nth listCntr attributeValTypeList))) ))
(setq listCntr (1+ listCntr)))
How can I dynamically build the association list.Any Ideas?
....
It looks like you're almost there. I believe you want < rather than <= there -- if there are 3 items in each list, the (length) function will return 3, but you want the counter to run only from 0 through 2.
And odd as it looks, if you're looking for a list that looks something like this:
((name0 val0) (name1 val1) (name2 val2) ...etc...)
then I think you need another list in there:
(setq listCntr 0)
(while (< listCntr (length attributeNameList))
(setq attrTagTypeAssnList
(append
attrTagTypeAssnList
(list
(list (nth listCntr attributeNameList) (nth listCntr attributeValTypeList))
); list
); append
); setq
(setq listCntr (1+ listCntr))
)
You could use (cons), but since that only adds a single item to the front of a list, as compared to (append) that will add together lists of any length, you would need to add each item at the beginning, and then (reverse) the final list. But it does let you reduce the number of (list) functions involved.
Can't find what you're looking for? Ask the community or share your knowledge.