Insert a new association

Discussion in 'AutoCAD' started by TCEBob, Jan 21, 2005.

  1. TCEBob

    TCEBob Guest

    For Mtext:
    .....
    (if(assoc 370 mt) ;make lineweight 0.35
    (setq mt(subst(cons 370 0.35)(assoc 370 mt) mt)) ;there is a 370 so change it
    (setq mt(subst((assoc 50 mt)(cons 370 0.35))(assoc 50 mt) mt)) ;not, so stick
    it in.
    ) ;if
    .....

    But something is wrong with the second option. Should I disect assoc 50 and then
    cons it? Append won't work (I think) because the new assoc will be in a bad
    location. Or does the system take care of that?

    rs
     
    TCEBob, Jan 21, 2005
    #1
  2. TCEBob

    Jeff Mishler Guest

    Looks like this works just fine:

    (setq ent (entget (car (entsel))))
    (setq ent (append ent '((370 . 35))))
    (entmod ent)
     
    Jeff Mishler, Jan 21, 2005
    #2
  3. TCEBob

    TCEBob Guest

    Thanks, Jeff. I could have tried that, too, but I really thought it would blow
    up.

    Just for the record -- is there a straightforward way of inserting a new list
    item in a specific location? Like
    (insert mylist exist new)?
    mylist==(a b c d e)
    exist==c
    new==W
    ==(a b c W d e)

    rs
     
    TCEBob, Jan 21, 2005
    #3
  4. TCEBob

    Joe Burke Guest

    Hi Bob,

    Is this straightforward? I don't think so, but it does the trick.

    (setq lst '("a" "b" "c" "d" "e"))
    (setq exist "c")
    (setq new "W")
    (setq lst1 (reverse (member exist (reverse lst))))
    (setq lst2 (cons new (cdr (member exist lst))))
    (append lst1 lst2)

    result:
    ("a" "b" "c" "W" "d" "e")

    Joe Burke
     
    Joe Burke, Jan 21, 2005
    #4
  5. In general, the approach that Joe posted will work on the
    example you show, but a word of caution is in order, so
    as to not let others mistakenly assume that it will work on
    any list.

    It works so long as the list does not contain duplicate
    replacement elements (and assuming you don't want to
    replace all of them, which is just a single call to subst).

    For example, it will fail with a list like this:

    '(A B C D E D C B A)

    Also, using reverse twice is expensive in terms of node
    consumption (reverse creates a copy of the input list),
    and while that's not too much of an issue with relatively
    small entity data lists, it can get very expensive with
    large lists.

    For (relatively small) entity data lists, I would generally
    advocate a recursive approach, since it's unlikely that it
    will exceed the stack limitations:

    (defun replace-first (new old lst)
    (if (member old lst)
    (replace-first-aux lst)
    )
    )

    (defun replace-first-aux (lst)
    (cond
    ( (not lst) nil)
    ( (equal (car lst) old)
    (cons new (cdr lst))
    )
    (t (cons (car lst)
    (replace-first-aux (cdr lst))
    )
    )
    )
    )
     
    Tony Tanzillo, Jan 21, 2005
    #5
  6. Oops. That should've been:

    (defun replace-first (new old lst)
    (if (member old lst)
    (replace-first-aux lst)
    lst
    )
    )
     
    Tony Tanzillo, Jan 21, 2005
    #6
  7. TCEBob

    TCEBob Guest

    Joe and Tony,

    Thanks for the input. I thought my idea of substituting c with c W would work,
    but it gave me problems. Plus it could get messy if c is not unique.

    rs
     
    TCEBob, Jan 21, 2005
    #7
  8. Just for the record -- is there a straightforward way of inserting a new
    This is my approach:

    (defun ALE_AppendAfterFirst (FrsItm NewItm In_Lst InRLst / NthPos)
    (if (setq In_Lst (member FrsItm In_Lst))
    (progn
    (setq NthPos (- (length InRLst) (length In_Lst)))
    (while
    (/=
    NthPos
    (length (setq InRLst (cdr (member FrsItm InRLst))))
    )
    )
    (append
    (reverse InRLst) (list FrsItm) (list NewItm) (cdr In_Lst)
    )
    )
    In_Lst
    )
    )

    (setq
    mylist '("a" "b" "c" "d" "c" "e" "c")
    exist "c"
    new "W"
    )

    (ALE_AppendAfterFirst exist new mylist (reverse mylist))
    To replace the first item:

    ; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
    ; Function: ALE_ReplaceFirst
    ;
    ; Version 1.00 - 22/01/2005
    ;
    ; Description:
    ; returns a copy of the list with a new item substituted
    ; in place of the first old item in the list
    ; If NewItm = nil OldItm is removed
    ;
    ; Arguments:
    ; NewItm = An atom or list
    ; OldItm = An atom or list
    ; In_Lst = A list
    ; InRLst = Original list reversed
    ;
    ; Return Values:
    ; A list
    ;
    ; Examples:
    ; (setq alist '(0 1 2 3 4 3 5 3 6 3 3 7))
    ;
    ; (ALE_ReplaceFirst "NEW" 3 alist (reverse alist))
    ; Returns: (0 1 2 "NEW" 4 3 5 3 6 3 3 7)
    ;
    ; (ALE_ReplaceFirst '(9 . Z) 3 alist (reverse alist))
    ; Returns: (0 1 2 (9 . Z) 4 3 5 3 6 3 3 7)
    ;
    ; (ALE_ReplaceFirst nil 3 alist (reverse alist))
    ; Returns: (0 1 2 4 3 5 3 6 3 3 7)
    ;
    (defun ALE_ReplaceFirst (NewItm OldItm In_Lst InRLst / NthPos)
    (if (setq In_Lst (member OldItm In_Lst))
    (progn
    (setq NthPos (- (length InRLst) (length In_Lst)))
    (while
    (/=
    NthPos
    (length (setq InRLst (cdr (member OldItm InRLst))))
    )
    )
    (append (reverse InRLst) (if NewItm (list NewItm)) (cdr In_Lst))
    )
    In_Lst
    )
    )
     
    Marc'Antonio Alessi, Jan 22, 2005
    #8
  9. TCEBob

    Joe Burke Guest

    Tony,

    I was vaguely aware what I suggested would not work with a list containing duplicate
    items.

    Thanks for the heads-up, and alternate solution.

    Joe Burke
     
    Joe Burke, Jan 23, 2005
    #9
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.