Author Topic: Increment Number  (Read 2400 times)

0 Members and 1 Guest are viewing this topic.

DanB

  • Bull Frog
  • Posts: 367
Increment Number
« 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.

Code: [Select]
(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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Increment Number
« Reply #1 on: March 03, 2009, 03:42:11 PM »
Another way
Code: [Select]
(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)
  )
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Increment Number
« Reply #2 on: March 03, 2009, 03:54:26 PM »
With a subroutine.
Code: [Select]
(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 reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Number
« Reply #3 on: March 03, 2009, 04:25:03 PM »
I've had variants of these general purpose justify/pad functions for years. Use / abuse to suit:

Code: [Select]
(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)

)

Code: [Select]
(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)

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

DanB

  • Bull Frog
  • Posts: 367
Re: Increment Number
« Reply #4 on: March 04, 2009, 09:25:47 AM »
Time permitting, I will take a look today at working these into my routine. Thanks for the replies.