Append List and Simplify

Discussion in 'AutoCAD' started by Jesús Padilla, Jan 21, 2004.

  1. I have the next list:
    '('("Table" . 45) '("Door" . 28) '("Star" . 56) '("Block" . 45) '("Floor" .
    28) '("Wall" . 56))

    and I need the next
    '('(45 . "Table" "Block") '(28 . "Door" "Floor") '(56 . "Wall" "Star"))

    ------------------------------------------------------------------------

    In the fisr list I have many assoc. Example: '("String" . Integer)

    In the second list I want to make new associations using the integer on the
    first element and append all the string that are associated with the same
    integer on the first list. Example: '(Integer . "String1" "String2")

    I have tried APPEND and CONS but it doesn´t work

    I Will Appreciate any help about.

    JPadilla
     
    Jesús Padilla, Jan 21, 2004
    #1
  2. Jesús Padilla

    Rudy Tovar Guest

    First what's it for?

    Next (cons 8 (list "it" "will" "work" "like" "this"))

    If you're planning on xdata, structure it by "," separation or individual
    components.
    --

    AUTODESK
    Authorized Developer
    www.Cadentity.com
    MASi
     
    Rudy Tovar, Jan 21, 2004
    #2
  3. This should help.

    (setq alst (list '("Table" . 45) '("Door" . 28) '("Star" . 56)
    '("Block" . 45)))
    (setq keys (vl-sort (mapcar 'cdr alst) '<))
    (foreach x keys
    (setq nlst (cons (cons x (kca_massoc x alst)) nlst))
    )


    (defun kca_massoc (key alist / x nlist)
    (foreach x alist
    (if (eq key (cdr x))
    (setq nlist (cons (car x) nlist))
    )
    )
    (reverse nlist)
    )

    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jan 21, 2004
    #3
  4. Do you mean ...

    Code:
    (defun CollectCdrs ( lst / result item key )
    
    (foreach pair
    
    (mapcar
    '(lambda (pair)
    (cons
    (cdr pair)
    (car pair)
    )
    )
    lst
    )
    
    (setq result
    (if
    (setq item
    (assoc
    (setq key (car pair))
    result
    )
    )
    (subst
    (cons
    key
    (list
    (cdr item)
    (cdr pair)
    )
    )
    item
    result
    )
    (cons
    pair
    result
    )
    )
    )
    )
    
    (reverse result)
    
    )
    
    (CollectCdrs
    '(  ("Table" . 45)
    ("Door" . 28)
    ("Star" . 56)
    ("Block" . 45)
    ("Floor" . 28)
    ("Wall" . 56)
    )
    )
    
    Returns ...
    
    (  (45 "Table" "Block")
    (28 "Door" "Floor")
    (56 "Star" "Wall")
    )
    
    
    While not in the dotted format you expressly identified, perhaps you can
    modify to suit.

    Cheers,

    Michael.


    I have the next list:
    '('("Table" . 45) '("Door" . 28) '("Star" . 56) '("Block" . 45) '("Floor" .
    28) '("Wall" . 56))

    and I need the next
    '('(45 . "Table" "Block") '(28 . "Door" "Floor") '(56 . "Wall" "Star"))

    ------------------------------------------------------------------------

    In the fisr list I have many assoc. Example: '("String" . Integer)

    In the second list I want to make new associations using the integer on the
    first element and append all the string that are associated with the same
    integer on the first list. Example: '(Integer . "String1" "String2")

    I have tried APPEND and CONS but it doesn´t work

    I Will Appreciate any help about.

    JPadilla
     
    michael puckett, Jan 21, 2004
    #4
  5. Jesús Padilla

    Doug Broad Guest

    Another one just for fun:
    (setq a (list '("Table" . 45)
    '("Door" . 28)
    '("Star" . 56)
    '("Block" . 45)
    '("Floor" . 28)
    '("Wall" . 56)
    '("Furniture" . 30)))

    (defun additem (itm lst)
    (if
    (atom lst)
    (list itm lst)
    (cons itm lst)))

    (defun reorganize (lst / newlst old)
    (foreach item lst
    (setq newlst
    (if (setq old (assoc (cdr item) newlst))
    (subst (cons (car old) (additem (car item) (cdr old))) old newlst)
    (cons (cons (cdr item) (car item)) newlst))))
    newlst)
     
    Doug Broad, Jan 21, 2004
    #5
  6. Now my problem is solved

    Rudy, Ken, Michael and Doug
    Thanks for your help

    JPadilla
     
    Jesús Padilla, Jan 21, 2004
    #6
  7. Jesús Padilla

    David Bethel Guest

    How a bout a singe call:

    (setq a (list '("Table" . 45)
    '("Door" . 28)
    '("Star" . 56)
    '("Block" . 45)
    '("Floor" . 28)
    '("Wall" . 56)
    '("Furniture" . 30)))
    (setq tmp nil)

    (foreach p a
    (cond ((assoc (cdr p) tmp)
    (setq tmp
    (subst (append (assoc (cdr p) tmp) (list (car p)))
    (assoc (cdr p) tmp) tmp)))
    (T (setq tmp (cons (list (cdr p) (car p)) tmp)))))

    (prin1 tmp)

    -David
     
    David Bethel, Jan 21, 2004
    #7
  8. Hi David

    Your function is great

    In this simple problem I learn that there are many ways to get the same
    goal

    Thanks

    JPadilla
     
    Jesús Padilla, Jan 21, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.