TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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.
(setq lst (list '("1" "2" "3" "test") '("a" "b" "c" "finished") '("a" "b" "c" "test")))
desired result to filter out "test"
_$ lst
(("a" "b" "c" "finished"))
desired result to filter out "finished"
_$ lst
(("1" "2" "3" "test")("a" "b" "c" "test"))
-
If case is not a concern perhaps ...
(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.
-
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.
(("1" "2" "3" "test")("a" "b" "c" "test"))
If case is not a concern perhaps ...
(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.
-
Wow, I misunderstood.
(vl-remove-if
'(lambda (x) (member "test" x))
'( ("1" "2" "3" "test")
("a" "b" "c" "finished")
("a" "b" "c" "test")
)
)
>> (("a" "b" "c" "finished"))
(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"))
-
You're a genius!
It works!
Here is my failed attempt.
(mapcar '(lambda (x)
(if (/= (vl-position "test" (nth x lst)) nil)
(vl-remove (nth x lst) lst)
(princ)
)
)lst)
Thanks!
Wow, I misunderstood.
(vl-remove-if
'(lambda (x) (member "test" x))
'( ("1" "2" "3" "test")
("a" "b" "c" "finished")
("a" "b" "c" "test")
)
)
>> (("a" "b" "c" "finished"))
(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"))
-
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.
-
Whilst by far the easiest method would be to make use of the vl-remove-if function, e.g.:
There are several other ways to tackle this particular task - for example, using basic iteration:
(defun foo2
( itm lst
/ rtn
) )
Or recursion:
-
Awesome! Thank you guys very much. I can understand vl-remove a little better now.
-
I was thinking the answer was "alphabetically".