TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on November 12, 2019, 12:44:51 PM

Title: How do I filter a list?
Post by: dubb on November 12, 2019, 12:44:51 PM
I am clueless as how to use vl-remove-if vl-member, etc. I would like to know how to filter a list based on an item. For instance. If the list contains "test" I want to remove it from the list and return a list without it.
Code: [Select]
(setq lst (list '("1" "2" "3" "test") '("a" "b" "c" "finished") '("a" "b" "c" "test")))
desired result to filter out "test"
Code: [Select]
_$ lst
(("a" "b" "c" "finished"))

desired result to filter out "finished"
Code: [Select]
_$ lst
(("1" "2" "3" "test")("a" "b" "c" "test"))
Title: Re: How do I filter a list?
Post by: MP on November 12, 2019, 01:18:32 PM
If case is not a concern perhaps ...

Code: [Select]
(defun foo ( item-to-remove lst )
    (if (atom (car lst))
        (vl-remove item-to-remove lst)
        (mapcar (function (lambda (x) (foo item-to-remove x))) lst)
    )
)

(foo "test"
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)

>>

(
    ("1" "2" "3")
    ("a" "b" "c" "finished")
    ("a" "b" "c")
)


Cheers.
Title: Re: How do I filter a list?
Post by: dubb on November 12, 2019, 01:29:34 PM
Like the the function but it only removes the string for me. I want to remove an entire list from the list.

Desired list if "finished" was filtered.

Code: [Select]
(("1" "2" "3" "test")("a" "b" "c" "test"))


If case is not a concern perhaps ...

Code: [Select]
(defun foo ( item-to-remove lst )
    (if (atom (car lst))
        (vl-remove item-to-remove lst)
        (mapcar (function (lambda (x) (foo item-to-remove x))) lst)
    )
)

(foo "test"
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)

>>

(
    ("1" "2" "3")
    ("a" "b" "c" "finished")
    ("a" "b" "c")
)


Cheers.
Title: Re: How do I filter a list?
Post by: MP on November 12, 2019, 01:34:27 PM
Wow,  I misunderstood.

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "test" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("a" "b" "c" "finished"))

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "finished" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("1" "2" "3" "test") ("a" "b" "c" "test"))

Title: Re: How do I filter a list?
Post by: dubb on November 12, 2019, 01:39:42 PM
You're a genius!
It works!

Here is my failed attempt.
Code: [Select]
(mapcar '(lambda (x)
(if (/= (vl-position "test" (nth x lst)) nil)
     (vl-remove (nth x lst) lst)
     (princ)
   )
)lst)


Thanks!
Wow,  I misunderstood.

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "test" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("a" "b" "c" "finished"))

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "finished" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("1" "2" "3" "test") ("a" "b" "c" "test"))
Title: Re: How do I filter a list?
Post by: MP on November 12, 2019, 02:05:11 PM
Once you get comfortable with the docs you’ll realize I’m not. Sorry for the scenic route, I frequently read WAY too fast and think I’ve the gist ...

Cheers.
Title: Re: How do I filter a list?
Post by: Lee Mac on November 12, 2019, 05:25:04 PM
Whilst by far the easiest method would be to make use of the vl-remove-if function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo1 ( itm lst )
  2.     (vl-remove-if '(lambda ( x ) (member itm x)) lst)
  3. )

There are several other ways to tackle this particular task - for example, using basic iteration:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo2 ( itm lst / rtn )
  2.     (foreach x lst (or (member itm x) (setq rtn (cons x rtn))))
  3.     (reverse rtn)
  4. )

Or recursion:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo3 ( itm lst )
  2.     (if lst
  3.         (if (member itm (car lst))
  4.             (foo3 itm (cdr lst))
  5.             (cons (car lst) (foo3 itm (cdr lst)))
  6.         )
  7.     )
  8. )
Title: Re: How do I filter a list?
Post by: dubb on November 12, 2019, 06:00:15 PM
Awesome! Thank you guys very much. I can understand vl-remove a little better now.
Title: Re: How do I filter a list?
Post by: Greg B on November 13, 2019, 08:52:31 AM
I was thinking the answer was "alphabetically".