TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on November 04, 2003, 08:11:33 AM
-
A few of ways to remove items from a list:
; 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")
; 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))
)
(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))
-
A couple more.
; 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))
; 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))
-
Another one. I hope these make sense, if not speek up.
; 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)
-
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):
(defun removeq (item lst)
(cond ((null lst) nil)
((equal item (car lst)) (removeq item (cdr lst)))
(t (cons (car lst) (removeq item (cdr lst))))
)
)
-
Excellent Stig! I love those recursion routines. They can be real brain teasers at times.
-
; 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
)
)
-
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>"))))
; 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