Home > Community > Forums > Custom IC SKILL > Lists: remove number X element

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: *

 Lists: remove number X element 

Last post Fri, Sep 20 2013 1:17 AM by Ionutz. 2 replies.
Started by Ionutz 19 Sep 2013 05:20 AM. Topic has 2 replies and 278 views
Page 1 of 1 (3 items)
Sort Posts:
  • Thu, Sep 19 2013 5:20 AM

    • Ionutz
    • Not Ranked
    • Joined on Tue, Nov 16 2010
    • Posts 7
    • Points 110
    Lists: remove number X element Reply

     Hi,

     

     Is there a way to remove a specific element from a list by knowing his location in the list and specifying to remove the elment from that location? :(

     Thank U,

    Ionut

    • Post Points: 20
  • Thu, Sep 19 2013 2:56 PM

    Re: Lists: remove number X element Reply

    One way would be to do:

    procedure(CCFremoveByIndex(lst n)
      let(((count 0))
        setof(elem lst (count++!=n))
      )
    )

    The second argument is the position to remove, where 0 is the first entry in the list.

    Another alternative is to use destructive methods:

    procedure(CCFremoveByIndex(lst n)
      let((remainder)
        if(zerop(n) then
          cdr(lst)
        else
          remainder=nthcdr(n-1 lst)
          rplacd(remainder cddr(remainder))
          lst
       )
      )
    )

    I've not profiled these, but the second generates less garbage, because it modifies the list in place, whereas the first constructs a new list.

    That said, it's a slightly odd thing to do - generally if you're accessing lists via a numbered index, it often means you're using the wrong approach. Lists are sequential data structures, and so are not really well suited to be accessed in a "random access" manner.

    Regards,

    Andrew.

    • Post Points: 20
  • Fri, Sep 20 2013 1:17 AM

    • Ionutz
    • Not Ranked
    • Joined on Tue, Nov 16 2010
    • Posts 7
    • Points 110
    Re: Lists: remove number X element Reply

     Hi Andrew,

     

     Thank U for the feedback... just started using Skill and am a bit confused when U say "wrong approach" to access lists via an index :(

     What I have at the moment are two lists: first with the main elements and second with parametter for each element in the first list (same index in the lists). When accessing element X from first list, I am also reading element 3 from second list to use the pair.

      With the remove option, all I really want is to make sure that I can access element 3 from both lists and remove them accordingl.

     From your experience, is there any risk that the list will not keep the order of the elements as they were added to the lists? :(

     

     Thank U,

    Ionut

     

    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by Ionutz at 19 Sep 2013 05:20 AM. Topic has 2 replies.