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 (151 Views)

appending an associative list

151 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,795
Registered: ‎09-13-2004
Message 2 of 3 (147 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 (119 Views)

Re: appending an associative list

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

thank you so much , seems embarrasingly obvious now !

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!