Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Contributor
insiaiftiqhar
Posts: 19
Registered: ‎06-21-2012
Message 1 of 3 (146 Views)

appending an associative list

146 Views, 2 Replies
07-17-2012 03:27 PM

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

*Expert Elite*
Kent1Cooper
Posts: 5,370
Registered: ‎09-13-2004
Message 2 of 3 (142 Views)

Re: appending an associative list

07-17-2012 05:34 PM 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
Contributor
insiaiftiqhar
Posts: 19
Registered: ‎06-21-2012
Message 3 of 3 (114 Views)

Re: appending an associative list

07-18-2012 12:08 PM in reply to: Kent1Cooper

thank you so much , seems embarrasingly obvious now !

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.