TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on June 11, 2009, 11:35:35 AM

Title: searching a list
Post by: andrew_nao on June 11, 2009, 11:35:35 AM
i have the following code to search a text file for a keyword
and makes a list of those keywords, it works but im looking to modify it to search a list for keywords and then make a new list of the keywords

can anyone help?
i appreciate any help and guidance

Code: [Select]
 
(setq TextFile "test.txt"))
   (setq SearchStr "revision")
   (setq Opened (open Textfile "r"))

  (while (setq tmpLine (read-line Opened))
   (if (vl-string-search (strcase SearchStr) (strcase tmpLine))
    (setq EndList (cons tmpLine EndList))
   )
  )
Title: Re: searching a list
Post by: Lee Mac on June 11, 2009, 11:45:12 AM
Not sure if maybe this would work:

Code: [Select]

(setq kLst  (mapcar 'strcase '("revision" "key2" "key3"))
      ofile (open "test.txt" "r"))

(while (setq nl (read-line ofile))
  (if (vl-some
        (function
          (lambda (x)
            (vl-string-search x (strcase nl)))) kLst)
    (setq EndList (cons nl EndList))))

(close ofile)
Title: Re: searching a list
Post by: andrew_nao on June 11, 2009, 11:59:29 AM
thanks for your reply

i guess i wasnt detailed enough
what im looking for is to search and existing list,
not open a text file and search it.
Title: Re: searching a list
Post by: T.Willey on June 11, 2009, 12:04:37 PM
Use ' vl-remove-if-not ' and test each item for what you want.  If it doesn't match, then it will be removed from the list.

Example
Code: [Select]
(vl-remove-if-not '(lambda ( x ) (equal x 1)) '(1 2 3 5 1 2 4 5 9 12))
return = (1 1)
Title: Re: searching a list
Post by: CAB on June 11, 2009, 12:13:50 PM
Andrew, You're request is not clear to me.

Would you post a before list & an after list so we can see what you want?
Title: Re: searching a list
Post by: MP on June 11, 2009, 12:20:27 PM
<ding> <ding>
Title: Re: searching a list
Post by: andrew_nao on June 11, 2009, 01:17:00 PM
Andrew, You're request is not clear to me.

Would you post a before list & an after list so we can see what you want?



example would be
the text file would contain:
something     something    revision1
something     something    revision2
something     something    revision3
something     something
something     something

if the code i posted is loaded it would return
something     something    revision1
something     something    revision2
something     something    revision3

i would like to to do the same thing but search a list not a text file and make a new list with the keyword
Title: Re: searching a list
Post by: andrew_nao on June 11, 2009, 01:21:26 PM
Use ' vl-remove-if-not ' and test each item for what you want.  If it doesn't match, then it will be removed from the list.

Example
Code: [Select]
(vl-remove-if-not '(lambda ( x ) (equal x 1)) '(1 2 3 5 1 2 4 5 9 12))
return = (1 1)
thanks Tim,

if my list is in a variable
how would i put my list in this formula?

Title: Re: searching a list
Post by: CAB on June 11, 2009, 01:38:30 PM
As Tim said:
Code: [Select]
(setq lst '("something     something    revision1"
            "something     something    revision2"
            "something     something    revision3"
            "something     something             "
            "something     something             "
           )
)

(vl-remove-if-not '(lambda (x) (wcmatch (strcase x) "*REVISION*")) lst)

Returns this

("something     something    revision1"
  "something     something    revision2"
  "something     something    revision3"
)
Title: Re: searching a list
Post by: andrew_nao on June 11, 2009, 01:56:50 PM
never mind, i see what i did wrong


thanks for everyones help

1 more small questioncan someone explain what lambda means?

Title: Re: searching a list
Post by: Lee Mac on June 11, 2009, 07:40:28 PM
Lambda is just an anonymous function defined to perform an operation.
Title: Re: searching a list
Post by: kdub_nz on June 11, 2009, 07:51:29 PM
Lambda is just an anonymous function defined to perform an operation.

hehe .. easy to say :)

.. but like juggling with knives in the dark, sometimes not so easy ..  without lots of practice ..  :|
Title: Re: searching a list
Post by: ziko30 on June 11, 2009, 07:57:02 PM

Its explained very well here:

http://www.theswamp.org/index.php?topic=2953.msg37000#msg37000