PDA

View Full Version : Counting Characters


BoKirra
2009-03-13, 01:51 AM
Hi ALL,

Supposed I need to count the number of characters which I input at the command prompt.
Here is a sample:
(setq test_1 (getstring "Please Enter Characters: ")
(if (< (strlen "test_1") 3)
(setq test_1
(getstring "Please enter not less than 3 characters : "))
) ;end of if
What is the problem with the code?
Thanks in advance.

RobertB
2009-03-13, 02:06 AM
You need to loop until the input is acceptable or the user cancels.

(defun C:Test (/ inp)
(while (and (< (strlen (setq inp (getstring "\nSpecify text or Enter to exit: "))) 3)
(/= inp ""))
(princ "\nNote: You must specify at least 3 characters."))
(cond ((/= inp "") (princ "\nValid input, continue code."))
((princ "\nHandle exit.")))
(princ))

BoKirra
2009-03-13, 03:23 AM
You need to loop until the input is acceptable or the user cancels.

(defun C:Test (/ inp)
(while (and (< (strlen (setq inp (getstring "\nSpecify text or Enter to exit: "))) 3)
(/= inp ""))
(princ "\nNote: You must specify at least 3 characters."))
(cond ((/= inp "") (princ "\nValid input, continue code."))
((princ "\nHandle exit.")))
(princ))

Thanks, Robert.
It's a "WHILE" function, again! :Oops:

peter
2009-03-14, 04:34 PM
Using the GRREAD expression you can accept only 3 characters in an input. You would probably need to add additional checking to filter out characters you may not want to accept as input.


(defun C:X1 ()
(setq str "")
(while (< (strlen str) 3)
(if (= 2 (car (setq lst (grread))))
(progn
(if (= (setq int (cadr lst)) 8)
(setq str (substr str 1 (1- (strlen str))))
(setq str (strcat str (chr (cadr lst))))
)
(princ "\r \r")
(princ str)
)
)
)
(princ "\n")
str
)

BoKirra
2009-03-16, 04:59 AM
Using the GRREAD expression you can accept only 3 characters in an input. You would probably need to add additional checking to filter out characters you may not want to accept as input.


(defun C:X1 ()
(setq str "")
(while (< (strlen str) 3)
(if (= 2 (car (setq lst (grread))))
(progn
(if (= (setq int (cadr lst)) 8)
(setq str (substr str 1 (1- (strlen str))))
(setq str (strcat str (chr (cadr lst))))
)
(princ "\r \r")
(princ str)
)
)
)
(princ "\n")
str
)


Thanks, Peter.
Hope I've learnt something from you.