TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: DanB on March 03, 2009, 03:29:36 PM
-
Feeling very rusty and haven't done any "lisping" in a long time so be kind...
Part of a larger routine, I need the output to be in the following format: anything less than "10" should read 001, 002, 003, etc. and <=10 should read 010, 011, 012, etc.
Because of the need to have (setq cnt (1- cnt)) - dealing with a list of numbers in routine - the entry "10" sneaks by and becomes "0010" - think I am missing something simple here.
(defun c:test (/ )
(setq cnt (getint "\nEnter Starting Number: "))
(setq cnt (1- cnt))
(if (<= cnt 9)
(setq txt1 (strcat "00" (itoa (setq cnt (1+ cnt)))))
(setq txt1 (strcat "0" (itoa (setq cnt (1+ cnt)))))
)
(princ txt1)
(princ)
)
]
Won't be able to check back until tomorrow - thanks for any feedback.
Dan
-
Another way
(defun c:test (/ )
(setq cnt (getint "\nEnter Starting Number: "))
(setq num$ (strcat "000" (itoa cnt))
num$ (substr num$ (- (strlen num$) 2) 3)
)
(princ num$)
(princ)
)
-
With a subroutine.
(defun c:test (/ )
(setq cnt (getint "\nEnter Starting Number: "))
(princ (padL (itoa cnt) 3 "0")) ; pad with 3 zeros
(princ)
)
;; CAB 03/03/09
(defun padL (word len chr$ / chr# pad)
(setq chr# (ascii chr$))
(repeat (- len (strlen word)) (setq pad (cons chr# pad)))
(strcat (vl-list->string pad) word)
)
-
I've had variants of these general purpose justify/pad functions for years. Use / abuse to suit:
(defun lset ( str white len )
;; left justify str, padding the result with
;; white chars for a total string ength of len
;;
;; (lset "left" "_" 10) => "left______"
(while (< (strlen white) len) (setq white (strcat white white)))
(substr (setq str (strcat str white)) 1 len)
)
(defun rset ( str white len )
;; right justify str, padding the result with
;; white chars for a total string ength of len
;;
;; (rset "right" "_" 10) => "_____right"
(while (< (strlen white) len) (setq white (strcat white white)))
(substr (setq str (strcat white str)) (- (strlen str) len -1))
)
In your case: (rset (itoa cnt) "0" 3)
-
Time permitting, I will take a look today at working these into my routine. Thanks for the replies.