Author Topic: Dynamic typing to extract 1 string from a list of strings  (Read 2495 times)

0 Members and 1 Guest are viewing this topic.

David Bethel

  • Swamp Rat
  • Posts: 656
Dynamic typing to extract 1 string from a list of strings
« on: February 25, 2015, 04:03:39 PM »
Greetings,

I'm trying to cut down on my embarrassing typos of customer names ( also expanding my laziness )

My goal is to type the minimum characters in order to extract a single name

So far rhis kludge is about as ugly as ( fill in he blank )

In vanilla Autolisp :

Code: [Select]
  (setq nl (list "John Doe" "Jane Doe" "Joe Doe" "Jack Doe"
                 "John Dot" "Jane Dot" "Joseph Don"
                 "Mike Smith" "May Smith" "Mark Smith"
                 "Don Jones" "Doc Jones" "Don Jock" "Don Zero"))

  (getname nl)

Code: [Select]
;;;ARG ->  SrtList
;;;RET -> 'STR
;;;ERR (exit)

(defun getname (sl / p tl c p tl)
  (setq sl (mapcar 'strcase sl))
  (textscr)
  (setq p "")
  (while (/= (length sl) 1)
         (setq c (strcase (getstring t (strcat "\n\nContact Name: " p " :   "))))
         (setq p (strcat p c)
               tl nil)
         (foreach a sl
            (if (= p (substr a 1 (strlen p)))
                (setq tl (cons a tl))))
         (setq sl tl)
         (cond ((not sl)
                (alert (strcat p " Not Found"))
                (exit))
               ((< (length sl) 15)
                (textpage)
                (foreach c sl
                  (princ (strcat "\n" c))))))

  (princ (strcat "\n" (car sl)))

  (car sl))

Any suggestions ?  TIA  -David



[/code]
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Dynamic typing to extract 1 string from a list of strings
« Reply #1 on: February 25, 2015, 06:14:15 PM »
Maybe something like this?

Code: [Select]
(defun getname ( msg lst / itm key len str )
    (princ msg)
    (setq lst (mapcar 'strcase lst)
          str ""
          len 0
    )
    (while (and (= 2 (car (setq key (grread nil 10)))) (/= 13 (setq key (cadr key))))
        (repeat len (princ "\010\040\010"))
        (cond
            (   (= 8 key)
                (if (/= "" str)
                    (princ (setq str (substr str 1 (setq len (1- (strlen str))))))
                )
            )
            (   (<= 32 key 126)
                (setq str (strcat str (strcase (chr key)))
                      len (strlen (princ (cond ((setq itm (getmatch (strcat str "*") lst))) (str))))
                )
            )
        )
    )
    itm
)
(defun getmatch ( str lst / itm )
    (while (and (setq itm (car lst)) (not (wcmatch itm str)))
        (setq lst (cdr lst))
    )
    itm
)
Code: [Select]
(getname "\nEnter a name: " nl)
« Last Edit: February 25, 2015, 06:20:10 PM by Lee Mac »

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Dynamic typing to extract 1 string from a list of strings
« Reply #2 on: February 26, 2015, 03:47:22 AM »
Very nice Lee !

Backspace editing as well !

Works with duplicate names !

Very quick !

Thank you again !  -David
R12 Dos - A2K

kruuger

  • Swamp Rat
  • Posts: 637
Re: Dynamic typing to extract 1 string from a list of strings
« Reply #3 on: February 26, 2015, 03:56:17 AM »
Maybe something like this?
very cool ! :)
kruuger

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Dynamic typing to extract 1 string from a list of strings
« Reply #4 on: February 26, 2015, 05:24:59 AM »
Cheers guys!  :-)