Author Topic: Does List Contain Text  (Read 3241 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Does List Contain Text
« on: August 01, 2011, 01:34:33 PM »
Ok, so I have a list and a string. The list contains wild cards and I need to determine if the string is in the list, allowing for the use of wild cards.

Here is where I set my list:
Code: [Select]
(setq TstList (List "*demo notes*" "*general notes*" "*installation notes*" "*reference notes*"))
So, I will give an example string that should return true when checked with the list:
Code: [Select]
(setq TstString "These are some general notes, this text should be found in the list.")
I am at a loss as to where to even begin on this one. I know that I could just do wcmatches manually, but the list may change over time, so I want to make it where I will need to make as few modifications as possible to the routine when that happens.


Edit: Modified test list, I had left in an extra wild card.....oops.
« Last Edit: August 01, 2011, 05:57:52 PM by cmwade77 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Does List Contain Text
« Reply #1 on: August 01, 2011, 02:00:04 PM »
I'm worthless with AutoLisp these days but i can complicate the concept as well as anyone i suppose. BTW, this should be super slow.

Code: [Select]
(defun instr? (sstr str c-sen / a)
  (cond ((eq c-sen nil) (setq sstr (strcase sstr))
               (setq str (strcase str))))
  (defun instr-nest (sstr str)
    (cond ((> (strlen str) 0)
           (if (eq sstr (substr str 1 (strlen sstr)))
             str
             (instr-nest sstr (substr str 2))))))
  (setq a (instr-nest sstr str))
  (if a (1+ (- (strlen str) (strlen a))) nil) )

(defun strParse (aStr delim / strList pos)
  (while
    (setq pos (vl-string-search delim aStr 0))
    (setq strList (cons (substr aStr 1 POS) strList)
          aStr (substr aStr (+ pos 2)))
    )
  (reverse (cons aStr strList))
 )

(setq TstList (List "*demo notes*" "*general notes*" "*installation notes*" "*reference notes*" "*"))
(setq TstString "These are some general notes, this text should be found in the list.")


(apply
  'append
  (mapcar
    (function
      (lambda (y)
(mapcar
  (function
    (lambda (x)
      (instr? x y nil)
    )
  )
  (strParse TstString " ")
)
      )
    )
    TstList
  )
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Does List Contain Text
« Reply #2 on: August 01, 2011, 02:04:38 PM »
Thinking about it some more, here is my attempt, although I think if the list gets long, it will be slow:
Code: [Select]
(defun nt_ListCheck (TstList TstString / Result Item)
    (foreach Item TstList
           (cond
                  ((wcmatch TstString Item)
                        (setq Result T)
                  )
           )
    )
Result
)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Does List Contain Text
« Reply #3 on: August 01, 2011, 02:08:12 PM »
first one is case sensitive...

Code: [Select]
(defun match? (lst string) (vl-some '(lambda (match) (wcmatch string match)) lst))
Code: [Select]
(defun match? (lst string)
  (vl-some '(lambda (match) (wcmatch (strcase string) (strcase match))) lst)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

kpblc

  • Bull Frog
  • Posts: 396
Re: Does List Contain Text
« Reply #4 on: August 01, 2011, 03:31:22 PM »
Another one?
Code: [Select]
(defun test (/ tstlist tststring)

  (setq tstlist (list "*demo notes*" "*general notes*" "*installation notes*" "*reference notes*" "*"))
  (setq tststring "These are some general notes, this text should be found in the list.")
  (wcmatch tststring
   (strcat (car tstlist) (apply 'strcat (mapcar '(lambda (x) (strcat "," x)) (cdr tstlist))))
   ) ;_ end of wcmatch
  ) ;_ end of defun
Sorry for my English.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Does List Contain Text
« Reply #5 on: August 01, 2011, 03:52:31 PM »
first one is case sensitive...

Code: [Select]
(defun match? (lst string) (vl-some '(lambda (match) (wcmatch string match)) lst))
Code: [Select]
(defun match? (lst string)
  (vl-some '(lambda (match) (wcmatch (strcase string) (strcase match))) lst)
)
This will always return T because of the last item in lst...
So, cmwade, what is the result you are looking for?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Does List Contain Text
« Reply #6 on: August 01, 2011, 05:55:36 PM »
Are you sure about that?  :-o
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.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Does List Contain Text
« Reply #7 on: August 01, 2011, 05:56:43 PM »
There was an error in my list example, I have fixed it in the first post.

Alan's code seems to be doing exactly what I need it to, now if I can just get the rest of my code to work right, I'll be doing good.....LOL.

Thank you all.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Does List Contain Text
« Reply #8 on: August 02, 2011, 01:33:42 AM »
Are you sure about that?  :-o
alanjt's function is OK but in OP, last item in TstList is was "*"
(wcmatch "some text" "*") is True for any string:
Code: [Select]
_$ (wcmatch "text" "*")
T
_$ (wcmatch "123" "*")
T
_$ (wcmatch "" "*")
T

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Does List Contain Text
« Reply #9 on: August 02, 2011, 07:41:55 AM »
Another, Vanilla LISP version, perhaps for those users unfortunate enough to be using a Mac  :-P

Code: [Select]
(defun _MatchInList ( str lst )
  (and lst (or (wcmatch str (car lst)) (_MatchInList str (cdr lst))))
)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Does List Contain Text
« Reply #10 on: August 02, 2011, 07:50:13 AM »
for those users unfortunate enough to be using a Mac  :-P
:lmao:


JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Does List Contain Text
« Reply #11 on: August 02, 2011, 10:16:26 AM »
I got a few minutes this morn so I cobbled together a VL method (man, I am rusty at lisp *phew*).
Code: [Select]
(setq TstList (List "*demo notes*" "*general notes*" "*installation notes*" "*reference notes*"))
(setq TstString "These are some general notes, this text should be found in the list.")

(mapcar
  (function
    (lambda ( x )
      (vl-string-search x TstString)))
  (mapcar
    (function
      (lambda ( x )
  (vl-string-trim "*" x)))
    TstList) )
Returns the string and the position. This one should "compete"; if I remember right, WCMATCH is the fastest method so I put the word `compete' in quotes.

BTW, was this supposed to be just a Boolean return?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Does List Contain Text
« Reply #12 on: August 02, 2011, 10:47:15 AM »
Sorry Stefan I thought you were referring to Alan's code and not the original list.
I removed "*" in my test because it looked like an obvious error.

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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Does List Contain Text
« Reply #13 on: August 02, 2011, 10:53:37 AM »
Why would you have a "*" by itself to begin with?
You could always add (vl-remove "*" lst) before processing the list with vl-some.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Does List Contain Text
« Reply #14 on: August 02, 2011, 03:22:13 PM »
Why would you have a "*" by itself to begin with?
You could always add (vl-remove "*" lst) before processing the list with vl-some.
The * by it's self was a residual of an older version of what I am working on, so it broke some things here, but I will need to put it back in, so I will keep in mind your vl-remove idea, thank you.