Author Topic: How do I filter a list?  (Read 2852 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
How do I filter a list?
« 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"))

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How do I filter a list?
« Reply #1 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.
« Last Edit: November 12, 2019, 01:22:30 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dubb

  • Swamp Rat
  • Posts: 1105
Re: How do I filter a list?
« Reply #2 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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How do I filter a list?
« Reply #3 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"))

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dubb

  • Swamp Rat
  • Posts: 1105
Re: How do I filter a list?
« Reply #4 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"))

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How do I filter a list?
« Reply #5 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How do I filter a list?
« Reply #6 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. )

dubb

  • Swamp Rat
  • Posts: 1105
Re: How do I filter a list?
« Reply #7 on: November 12, 2019, 06:00:15 PM »
Awesome! Thank you guys very much. I can understand vl-remove a little better now.

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: How do I filter a list?
« Reply #8 on: November 13, 2019, 08:52:31 AM »
I was thinking the answer was "alphabetically".