Home > Community > Forums > PCB SKILL > Remove item from list

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Remove item from list 

Last post Sun, Aug 13 2006 7:58 PM by archive. 4 replies.
Started by archive 13 Aug 2006 07:58 PM. Topic has 4 replies and 2240 views
Page 1 of 1 (5 items)
Sort Posts:
  • Sun, Aug 13 2006 7:58 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    Remove item from list Reply

    I'm trying to remove some items which both in two lists.
    But the remove command doesn't work.

    A='(0 1 2 3 4 5 6 7 8 9)
    B='(1 3 5 7 9)
    C=nil
    foreach(item A
                 if(member(item B) then
                    C=remove(item A)
                   )
                )


    why the result is C= (0 1 2 3 4 5 6 7 8) not (0 2 4 6 8)?
    how the remove works?

    Who can help to tell me the reason or share me a right code to make it true~~
    Thanks.


    Originally posted in cdnusers.org by leonlee
    • Post Points: 0
  • Sun, Aug 13 2006 8:22 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: Remove item from list Reply

    I got the right result by using [b] copy [/b] and [b] remd [/b]

    A='(0 1 2 3 4 5 6 7 8 9)
    B='(1 3 5 7 9)
    C=copy(A)
    foreach(item A
    if(member(item B) then
    remd(item C)
    )
    )


    Originally posted in cdnusers.org by leonlee
    • Post Points: 0
  • Sun, Aug 13 2006 8:30 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: Remove item from list Reply

    Hi Leon,

    remove is a destructive function so you were correct to use copy.
    Another (faster) method is to use cons, to create a new list, or the slowish following method
    C = setof(item, A, !member(item, B))

    Cheers, Dave


    Originally posted in cdnusers.org by Dave Elder
    • Post Points: 0
  • Sun, Aug 13 2006 10:03 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: Remove item from list Reply

    Thanks Dave,
    I havn't think of !member,
    At first, I used member(item B)=nil, it doesn't work. Haha~~


    Originally posted in cdnusers.org by leonlee
    • Post Points: 0
  • Thu, Sep 21 2006 1:27 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: Remove item from list Reply

    below is what I use, it is very fast and does not go through unnecessary loops if you have duplicates in list A (or if all items in A have already been removed)

    A='(1 2 1 3 1 4 1 5)
    B='(1 3 5)

    procedure(SubstractList(B A)
    let((item)
    while(B && A
    item=car(B)
    B=cdr(B)
    A=remove(item A)
    )
    A
    )
    )

    Note that both lists are modified inside the procedure. This is ok since they are copies.
    If both lists are dbids, then creating another procedure that uses "req" instead of "remove" will be even faster.


    Originally posted in cdnusers.org by natebizu
    • Post Points: 0
Page 1 of 1 (5 items)
Sort Posts:
Started by archive at 13 Aug 2006 07:58 PM. Topic has 4 replies.