Author Topic: searching a list  (Read 2051 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
searching a list
« on: March 12, 2012, 11:26:29 AM »
im trying to search a list for a specifc line but instead of returning the line its printing the whole list.

what am i missing?

it works properly if i write the list to a text file but im trying to avoid that.

Code: [Select]
(defun Stock1 (SearchStr / TextFile tmpLine EndList Opened)

(while (setq tmpLine (read-line mylist))
(if (vl-string-search (strcase SearchStr) (strcase tmpLine))
(setq EndList (cons tmpLine EndList))
)
)

(if EndList
(foreach i EndList
(princ (nth 0 endlist))
)
)
)

(princ)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: searching a list
« Reply #1 on: March 12, 2012, 11:31:47 AM »
I would assume that you haven't posted the code in its entirety, since the variable 'mylist' isn't initialised in the code, furthermore, the file descriptor that is apparently bound to the variable 'mylist' isn't closed.

In your final foreach expression, note that it will iterate over every item in the list 'EndList', but will only print the first item [(nth 0 endlist)] in each iteration.

andrew_nao

  • Guest
Re: searching a list
« Reply #2 on: March 12, 2012, 11:45:38 AM »
I would assume that you haven't posted the code in its entirety, since the variable 'mylist' isn't initialised in the code,
thats correct, my list is 6000 lines lone
Quote
furthermore, the file descriptor that is apparently bound to the variable 'mylist' isn't closed.
i dont know what you mean by this

Quote
In your final foreach expression, note that it will iterate over every item in the list 'EndList', but will only print the first item [(nth 0 endlist)] in each iteration.

yes my plan was for it to read each line and if it matches the search term then (nth 0 endlist) will be the found result, if found.

as i said this works if it searches a text file, im just trying to get around having to use a text file

here it is with the text file code in it

Code: [Select]
(defun Stock1 (SearchStr / TextFile tmpLine EndList Opened)
(if
(and
(findfile (setq TextFile "C:\\test.txt"))
(setq Opened (open TextFile "r"))
)
(while (setq tmpLine (read-line Opened))
(if (vl-string-search (strcase SearchStr) (strcase tmpLine))
(setq EndList (cons tmpLine EndList))
)
)
)
(if EndList
(foreach i EndList
(princ (nth 0 endlist))
)
)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: searching a list
« Reply #3 on: March 12, 2012, 12:45:01 PM »
Something like this maybe?
Assumes that SearchLst is your list of strings and targetStr is what you are trying to find.
Code: [Select]
(defun stock1 (searchlst TargetStr)
  (setq TargetStr (strcase TargetStr))
  (foreach str searchLst
    (if (vl-string-search TargetStr (strcase str))
      (princ (strcat "\n" str))
    )
  )
)
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.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: searching a list
« Reply #4 on: March 12, 2012, 12:59:42 PM »
furthermore, the file descriptor that is apparently bound to the variable 'mylist' isn't closed.
i dont know what you mean by this

I mean that if 'mylist' is the file descriptor returned by the open function, you haven't released it with the close function.

as i said this works if it searches a text file, im just trying to get around having to use a text file

If you have just a list of strings, you could use the code suggested by CAB, or maybe something like:

Code - Auto/Visual Lisp: [Select]
  1. (setq searchstr (strcase searchstr))
  2. (vl-some '(lambda ( x ) (if (vl-string-search searchstr (strcase x)) x)) listofstrings)

andrew_nao

  • Guest
Re: searching a list
« Reply #5 on: March 12, 2012, 01:05:11 PM »
i think thats my problem, im expecting to search a list of strings when i think its a list of lists...

ill have to search more on this

thanks

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: searching a list
« Reply #6 on: March 13, 2012, 11:50:59 AM »
From your original post, it seems you're reading a text file into a list of text lines. But you're only adding each line if there's a match to the SearchStr. I'd actually go a different route: 1st read all the lines into a list. 2nd extract only those matching SearchStr. Somethnig like this:
Code - Auto/Visual Lisp: [Select]
  1. ;; Read file into list of lines
  2. (defun TextFile->List (filename /  fileHandle line returnList)
  3.   (if (and (setq filename (findfile filename)) (setq fileHandle (open filename "r")))
  4.     (progn
  5.       (while (setq line (read-line fileHandle)) (setq returnList (cons line returnList)))
  6.       (close f)
  7.     )
  8.   )
  9.   (reverse returnList)
  10. )
  11.  
  12. ;; Extract only items from list containing matching search string
  13. (defun Extract-Match (SourceList Search / )
  14.   (setq Search (strcase Search))
  15.   (vl-remove-if-not '(lambda (item) (vl-string-search Search (strcase item))) SourceList)
  16. )
Then to call them both in one shot:
Code - Auto/Visual Lisp: [Select]
  1. (setq FinalList (Extract-Match (TextFile->List "C:\\test.txt") "MyMatch"))
« Last Edit: March 13, 2012, 11:55:56 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

efernal

  • Bull Frog
  • Posts: 206
Re: searching a list
« Reply #7 on: March 13, 2012, 01:04:38 PM »
a suggestion...

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN stock1 (searchstr textfile / cn file line counter tempfile)
  2.   (IF (AND (FINDFILE textfile) (SETQ file (OPEN textfile "r")))
  3.     (PROGN
  4.       (SETQ searchstr (STRCASE searchstr)
  5.             cn        0
  6.             counter   0
  7.             tempfile  (OPEN "$$temp$$.txt" "w")
  8.       )
  9.       (WHILE (SETQ line (READ-LINE file))
  10.         (SETQ cn (1+ cn))
  11.         (IF (WCMATCH (STRCASE line) searchstr)
  12.           (PROGN
  13.             (WRITE-LINE
  14.               (STRCAT
  15.                 (ITOA cn)
  16.                 (CHR 9)
  17.                 line)
  18.               tempfile)
  19.             (SETQ counter (1+ counter)))
  20.         )
  21.       )
  22.       (CLOSE tempfile)
  23.       (CLOSE file)
  24.       (IF (> counter 0)
  25.         (STARTAPP
  26.           (STRCAT "NOTEPAD.EXE /A" (CHR 32) (FINDFILE "$$temp$$.txt")))
  27.         (PROGN
  28.           (VL-FILE-DELETE (FINDFILE "$$temp$$.txt"))
  29.           (PRINC "\n-> No matches found..."))
  30.       )
  31.     )
  32.     (PRINC (STRCAT "\n-> " textfile " not found..."))
  33.   )
  34.   (PRINC)
  35. )
  36.  
  37. ;; usage (stock1 "text to find" "C:\\temp\\temp.txt")
e.fernal

efernal

  • Bull Frog
  • Posts: 206
Re: searching a list
« Reply #8 on: March 13, 2012, 01:08:44 PM »
Please, change
Code - Auto/Visual Lisp: [Select]
  1.  
  2.  (VL-FILE-DELETE (FINDFILE "$$temp$$.txt"))
  3. ;; to
  4. (IF (FINDFILE "$$temp$$.txt")
  5.             (VL-FILE-DELETE (FINDFILE "$$temp$$.txt"))
  6.           )
  7.  
  8.  
e.fernal