123456abc 发表于 2010-4-26 22:04:00

求助自动数字递增

<p>请问如何实现下面的数字递增:</p><p>ABC-123-50改为ABC-124-50,也就是将一组数字中间的那个递增,其他不变,请高手指点啊,谢谢!!!</p>

Andyhon 发表于 2010-4-26 23:51:00

<p>Try this</p><p>(setq txt (ssget ":S" '((0 . "TEXT")))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt (ssname txt 0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dat (entget txt)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt (cdr (assoc 1 dat))<br/>)<br/>(entmod<br/>&nbsp; (subst<br/>&nbsp;&nbsp;&nbsp; (cons 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (strcat<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (substr txt 1 4)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (itoa (1+ (atoi (substr txt 5 3))))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (substr txt 8)&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (assoc 1 dat)<br/>&nbsp;&nbsp;&nbsp; Dat<br/>) )</p><p></p>

123456abc 发表于 2010-4-27 10:55:00

<p>非常感谢您的程序,确实可以用,但是我还有一个问题,就是如果我前面的字母个数不定,那应该如何做呢,还有可不可以实现连续操作,如下面的形式:ABC-001-50</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ABCD-001-80</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EF-001-100</p><p>经过连续操作以后,数字改为:ABC-002-50</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ABCD-003-80</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EF-004-100</p>

123456abc 发表于 2010-4-27 19:40:00

有高手能帮一下忙吗,这个估计难度不小

danxingpen 发表于 2010-4-28 10:46:00

找找以前的帖子,文本分割

xshrimp 发表于 2010-4-28 11:20:00

本帖最后由 作者 于 2010-5-7 22:06:15 编辑

;;; 解析字符串为表(函数来自明经通道转载)
;;; ---------------------------------------------------------------------------------
(defun strParse    (Str Delimiter / SearchStr StringLen return n char)
(setq SearchStr Str)
(setq StringLen (strlen SearchStr))
(setq return '())
(while (> StringLen 0)
    (setq n 1)
    (setq char (substr SearchStr 1 1))
    (while (and (/= char Delimiter) (/= char ""))
      (setq n (1+ n))
      (setq char (substr SearchStr n 1))
    ) ;_ end of while
    (setq return (cons (substr SearchStr 1 (1- n)) return))
    (setq SearchStr (substr SearchStr (1+ n) StringLen))
    (setq StringLen (strlen SearchStr))
) ;_ end of while
(reverse return)
) ;_ end of defun
;;; 反解析表为字符串(函数来自明经通道转载)
;;; ---------------------------------------------------------------------------------
(defun StrUnParse (Lst Delimiter / return)
(setq return "")
(foreach str Lst
    (setq return (strcat return Delimiter str))
) ;_ end of foreach
(substr return 2)
) ;_ end of defun
(defun c:tt()
(setq i (getint "\输入起始数字:"))
(while (setq txt (ssget ":S" '((0 . "TEXT"))))
(setq
txt (ssname txt 0)
dat (entget txt)
str (cdr (assoc 1 dat))
)

(setq strlst (strParse Str "-"))
(setq numws (strlen (nth 1 strlst)))
(setq txt_num_new (itoa i ))
(repeat (- numws (strlen txt_num_new))
      (setq txt_num_new(strcat "0" txt_num_new))
)

(setq nuwstr (StrUnParse (list (car strlst) txt_num_new (laststrlst)) "-"))
(entmod
    (subst
      (cons 1nuwstr)
      (assoc 1 dat)
      Dat
) )
(setq i (1+ i))
)
)

123456abc 发表于 2010-4-28 16:30:00

非常感谢楼上的高手,用起来很方便,再次谢谢你!!

123456abc 发表于 2010-5-7 20:32:00

<p>再次求助高手帮帮忙,如果是四组数据,6楼的程序该如何改写啊?</p><p>例如&nbsp; ABC-001-50-2A2.要使其中的001连续递增。谢谢各位了</p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p>、</p>

xshrimp 发表于 2010-5-7 22:18:00

稍微修改一下就可以了。适合
ABC-123-50
ABC-123-50-131
ABC-123-50-131-123212
其中123数字的递增
;;; 解析字符串为表(函数来自明经通道转载)
;;; ---------------------------------------------------------------------------------
(defun strParse    (Str Delimiter / SearchStr StringLen return n char)
(setq SearchStr Str)
(setq StringLen (strlen SearchStr))
(setq return '())
(while (> StringLen 0)
    (setq n 1)
    (setq char (substr SearchStr 1 1))
    (while (and (/= char Delimiter) (/= char ""))
      (setq n (1+ n))
      (setq char (substr SearchStr n 1))
    ) ;_ end of while
    (setq return (cons (substr SearchStr 1 (1- n)) return))
    (setq SearchStr (substr SearchStr (1+ n) StringLen))
    (setq StringLen (strlen SearchStr))
) ;_ end of while
(reverse return)
) ;_ end of defun
;;; 反解析表为字符串(函数来自明经通道转载)
;;; ---------------------------------------------------------------------------------
(defun StrUnParse (Lst Delimiter / return)
(setq return "")
(foreach str Lst
    (setq return (strcat return Delimiter str))
) ;_ end of foreach
(substr return 2)
) ;_ end of defun
;EF-001-100
(defun c:tt()
(setq i (getint "\输入起始数字:"))
(while (setq txt (ssget ":S" '((0 . "TEXT"))))
(setq
txt (ssname txt 0)
dat (entget txt)
str (cdr (assoc 1 dat))
)
;(setq str "ABC-001-50-2A2") ;test
(setq strlst (strParse Str "-"))
(setq numws (strlen (nth 1 strlst)))
(setq txt_num_new (itoa i ))
(repeat (- numws (strlen txt_num_new))
      (setq txt_num_new(strcat "0" txt_num_new))
)
(setq nuwstr (StrUnParse (append (list (car strlst))(list txt_num_new) (cddrstrlst)) "-"))
(entmod
    (subst
      (cons 1nuwstr)
      (assoc 1 dat)
      Dat
) )
(setq i (1+ i))
)
)

123456abc 发表于 2010-5-8 16:24:00

xshrimp太感谢你了,非常感谢你的帮助!!
页: [1] 2
查看完整版本: 求助自动数字递增