Author Topic: searching a list  (Read 2325 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
searching a list
« 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))
   )
  )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: searching a list
« Reply #1 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)

andrew_nao

  • Guest
Re: searching a list
« Reply #2 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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: searching a list
« Reply #3 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)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: searching a list
« Reply #4 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?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: searching a list
« Reply #5 on: June 11, 2009, 12:20:27 PM »
<ding> <ding>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

andrew_nao

  • Guest
Re: searching a list
« Reply #6 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

andrew_nao

  • Guest
Re: searching a list
« Reply #7 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?


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: searching a list
« Reply #8 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"
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

andrew_nao

  • Guest
Re: searching a list
« Reply #9 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?

« Last Edit: June 11, 2009, 02:08:51 PM by andrew_nao »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: searching a list
« Reply #10 on: June 11, 2009, 07:40:28 PM »
Lambda is just an anonymous function defined to perform an operation.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: searching a list
« Reply #11 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 ..  :|
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

ziko30

  • Newt
  • Posts: 51
Re: searching a list
« Reply #12 on: June 11, 2009, 07:57:02 PM »
============== Acad Map 3D 2009================