Author Topic: Removing items from a list  (Read 6497 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« on: November 04, 2003, 08:11:33 AM »
A few of ways to remove items from a list:

Code: [Select]

; remove all the words "one" from the list
(setq lst1 (list "one" "two" "three" "four" "five" "one")
      lst2 (vl-remove "one" lst1)
      )

; ("two" "three" "four" "five")


Code: [Select]

; remove the Z value from a point list while collecting them
(while
  (setq pt (getpoint "\nPick some points <enter to quit>: "))
  (setq pt-lst (cons (vl-remove (nth 2 pt) pt) pt-lst))
  )

 
Code: [Select]

(setq lst3 (list (cons 'w 34)(cons 'r 56)(cons 'h 76)(cons 'x 102)(cons 'q 33)(cons 'l 109)))

(setq new-lst (vl-remove-if '(lambda (i) (> (cdr i) 100)) lst3))

; from this ((W . 34) (R . 56) (H . 76) (X . 102) (Q . 33) (L . 109))
; to this ((W . 34) (R . 56) (H . 76) (Q . 33))
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« Reply #1 on: November 04, 2003, 08:34:07 AM »
A couple more.

Code: [Select]

; convert user input string to a list
; remove any uppercase letters
; put the string back together
(setq s-lst (vl-string->list (getstring T "\nEnter a string: ")))
(vl-list->string (vl-remove-if '(lambda (i) (and (< 65 i)(> 90 i))) s-lst))


Code: [Select]

; same as above, but removes the lowercase letters
(setq s-lst (vl-string->list (getstring T "\nEnter a string: ")))
(vl-list->string (vl-remove-if-not '(lambda (i) (and (< 64 i)(> 91 i))) s-lst))
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« Reply #2 on: November 04, 2003, 09:11:49 AM »
Another one. I hope these make sense, if not speek up.

Code: [Select]

; remove all items in the list that start with "234"
; using WCMATCH
(setq lst (list "234mark" "334john" "234craig" "344hendie" "234smadsen"))
(vl-remove-if '(lambda (i) (wcmatch i "234*")) lst)
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
Removing items from a list
« Reply #3 on: November 04, 2003, 09:21:16 AM »
Keep'em coming :)  They all show neat little tricks that are worth while to remember.

Here's a little bareboned remove function for those who are still in R14 (or just get a kick out of bypassing the vl- functions):

Code: [Select]
(defun removeq (item lst)
  (cond ((null lst) nil)
        ((equal item (car lst)) (removeq item (cdr lst)))
        (t (cons (car lst) (removeq item (cdr lst))))
  )
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« Reply #4 on: November 04, 2003, 09:26:13 AM »
Excellent Stig! I love those recursion routines. They can be real brain teasers at times.
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« Reply #5 on: November 05, 2003, 07:23:27 AM »
Code: [Select]

; collect a list of entities from the user
(while
  (setq ent (entsel "\nSelect some entities: "))
  (setq entlst (cons (entget (car ent)) entlst))
  (prompt
    (strcat "Total items selected equals: "(itoa (length entlst)))
    )
  )

; remove all entity lists that are not associated to a LINE
(setq entlst-lines
      (vl-remove-if-not
        '(lambda (i) (= (cdr (assoc 0 i)) "LINE"))
        entlst
        )
      )
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Removing items from a list
« Reply #6 on: November 05, 2003, 08:08:11 AM »
This is not meant as a practical solution only an example. The output of
this program can be achieved by adding a simple filter to the 'ssget'
function. i.e. (setq ss (ssget '((8 . "<layer name>"))))
Code: [Select]

; return a an entity list from a selection set of just
; those items on layer 'lay-name'
; lay-name = string
; example
; (setq lst (weedbylayer "layername"))
(defun weedbylayer (lay-name / cntr ss ent lst entlst-layer)

  (setq cntr 0
        ss (ssget)
        )

  (if ss
    (while
      (setq ent (ssname ss cntr))
      (setq lst  (cons (entget ent) lst)
            cntr (1+ cntr)
            )
      )
    )

  (setq entlst-layer
        (vl-remove-if-not
          '(lambda (i) (= (cdr (assoc 8 i)) lay-name))
          lst
          )
        )

  );defun
TheSwamp.org  (serving the CAD community since 2003)