• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Visual LISP, AutoLISP and General Customization

    Reply
    Contributor
    Posts: 18
    Registered: ‎06-21-2012

    appending an associative list

    75 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

    Please use plain text.
    *Expert Elite*
    Kent1Cooper
    Posts: 4,058
    Registered: ‎09-13-2004

    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
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎06-21-2012

    Re: appending an associative list

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

    thank you so much , seems embarrasingly obvious now !

    Please use plain text.