Author Topic: Filter list  (Read 1018 times)

0 Members and 1 Guest are viewing this topic.

Robert98

  • Guest
Filter list
« on: April 06, 2016, 09:16:58 AM »
Hello everyone.
I want to filter a list of strings just like the filter function in VB script ,but when I launch the code, the original list shows up.
Can anyone tell me what i'm doing wrong . Here is my code:
Code: [Select]
(vl-load-com)
(defun c:Filter   (/ lst1 elem elem2)

  (setq lst1 '("Study" "moon" "teacher" "ball"))
  (foreach elem   lst1
    (setq elem2 (vl-string->list elem))
    (if   (= (member '(ascii "m") elem2) nil)
      (vl-remove elem lst1)
      (princ "\n")
    )
  )
  (princ lst1)
  (princ)
)

When I fire up the filter command ,I expect to get ("Study" "teacher" "ball") which is a list without strings that contain "m" letter in their structure.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Filter list
« Reply #1 on: April 06, 2016, 10:03:52 AM »
When you remove the element, you need to set your lst1 variable like so:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst1 (vl-remove elem lst1))
Also ...
Code - Auto/Visual Lisp: [Select]
  1. (member '(ascii "m") elem2)
will always return nil because of the ' ( quote ).


This can also be achieved using wcmatch like so ( keep in mind wcmatch is case sensitive ):

Code - Auto/Visual Lisp: [Select]
  1. (vl-remove-if '(lambda (x) (wcmatch x "*m*")) '("Study" "moon" "teacher" "ball"))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Filter list
« Reply #2 on: April 06, 2016, 10:14:24 AM »
Put another way, (vl-remove ...) returns a modified copy of the list and leaves the original intact.  Most LISP functions operate that way.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

ChrisCarlson

  • Guest
Re: Filter list
« Reply #3 on: April 06, 2016, 10:26:08 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun CC:remove ( lst1 wc /  )
  2.         (setq lst2     
  3.                 (vl-remove-if
  4.                         '(lambda (x)
  5.                                 (wcmatch x wc))
  6.                         lst1
  7.                 )
  8.         )      
  9. )
  10.  
  11.  
  12. (defun c:remove_test ( / lst)
  13.         (setq lst '("Study" "moon" "teacher" "ball"))
  14.         (CC:remove lst "*m*")
  15. )
  16.  

Robert98

  • Guest
Re: Filter list
« Reply #4 on: April 06, 2016, 10:54:44 AM »
Thank you everyone for all your replies.
I'll try what you suggested and i'll provide you with more details on the results.