哪位大侠帮看看问题出在哪
我想提取一个txt的部份数据给程序赋值,格式为1,*********,**********,SXT
2,********,********,HLD
3,******,***********,XHD
4,*******,****,DHT
5,*******,*****,GPS
6,******,****,DXD
7,**********,****,MTGD
8,******,****,TGD
…………
…………
*为不同类型文字,需提出第三个逗号后面的数据为变量(不固定,经常作为参数修改)执行结果应为
bh1=SXT
bh2=HLD
bh3=XHD
……
……
bh1 bh2 bh3……为变量供程序调用,SXT HLD XHD……为参数随时修改,该如何实现
我下面这个程序在哪里出错
(defun c:tt ()
(setq blnm "bh")
(if (setq fp (open "C:\\bh.txt" "r"))
(progn
(setq i 1)
(while
(setq txt (read-line fp))
(setq j 1 n 0)
(while (< n 3)
(setq a (substr txt j 1))
(IF (= a ",")
(progn
(SETQ N (1+ N))
(cond
((and (= a ",") (= n 3))
(setq bm (substr txt (+ 1 j)))
)
((and (= a ",") (= n 1))
(setq bh (substr txt 1 (1- j)))
)
)
)
)
(setq j (1+ j))
)
(set (read (strcat blnm bh)) bm)
(setq i (1+ i))
)
(close fp)
)
)
)
望各位大侠指点
程序执行后
Command: !bh1
"SXT"
Command: !bh8
"TGD "
什么卡了? Andyhon 发表于 2017-10-26 10:42
程序执行后
Command: !bh1
我知道了,我原来的序号前面带0就会出错,如01,02,03……10,11……返回NIL,谢谢 本帖最后由 yxp 于 2017-10-26 11:46 编辑
(defun c:tt( / mt f n)
(setq f (open "c:\\bh.txt" "r") n 0)
(while (setq txt(read-line f))(setq mt (cons (car (SplitA txt ",")) mt)))
(close f)
(foreach x (reverse mt)(set (read (strcat "bh" (itoa(setq n(1+ n))))) x))
)
;;字符分割,通用函数
(defun SplitA (str p / pa sl xn)
(setq xn (1+ (strlen p)))
(while (setq pa (vl-string-search p str))
(setq sl (cons (substr str 1 pa) sl)
str (substr str (+ pa xn)))
)(cons str sl)
) 回帖是一种美德!感谢楼主的无私分享 谢谢 Ref:
(vl-load-com)
(defun c:tt ()
(if (setq fp (open "V:\\bh.txt" "r"))
(progn
(setq i 1)
(while (setq txt (read-line fp))
(setq txt (vl-string-right-trim " " txt)
idx (vl-string-position (ascii ",") txt nil T)
bm (substr txt (1+ (1+ idx)))
)
(set (read (strcat "bh" (itoa i))) bm)
(setq i (1+ i))
)
(close fp)
)
)
)
厉害,学习了 yxp 发表于 2017-10-26 11:26
学习了,谢谢
不知有没有小写改大写的通用函数 Returns a string where all alphabetic characters have been converted to uppercase or lowercase
(strcase string )
Arguments
string
A string.
which
If specified as T, all alphabetic characters in string are converted to lowercase. Otherwise, characters are converted to uppercase.
Return Values
A string.
Examples
Command: (strcase "Sample")
"SAMPLE"
页:
[1]