Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

appending an associative list

2 REPLIES 2
Reply
Message 1 of 3
insiaiftiqhar
411 Views, 2 Replies

appending an associative list

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

2 REPLIES 2
Message 2 of 3
Kent1Cooper
in reply to: insiaiftiqhar


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

Kent Cooper, AIA
Message 3 of 3
insiaiftiqhar
in reply to: Kent1Cooper

thank you so much , seems embarrasingly obvious now !

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost