明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 547|回复: 8

[提问] 哪位大侠帮看看问题出在哪

[复制链接]
发表于 2017-10-25 22:11:00 | 显示全部楼层 |阅读模式
我想提取一个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)
  )
)
)
望各位大侠指点
发表于 2017-10-26 10:42:02 | 显示全部楼层

程序执行后

Command: !bh1
"SXT"

Command: !bh8
"TGD    "

什么卡了?
 楼主| 发表于 2017-10-26 11:12:55 | 显示全部楼层
Andyhon 发表于 2017-10-26 10:42
程序执行后

Command: !bh1

我知道了,我原来的序号前面带0就会出错,如01,02,03……10,11……返回NIL,谢谢
发表于 2017-10-26 11:26:25 | 显示全部楼层
本帖最后由 yxp 于 2017-10-26 11:46 编辑

  1. (defun c:tt( / mt f n)
  2. (setq f (open "c:\\bh.txt" "r") n 0)
  3. (while (setq txt(read-line f))(setq mt (cons (car (SplitA txt ",")) mt)))
  4. (close f)
  5. (foreach x (reverse mt)(set (read (strcat "bh" (itoa(setq n(1+ n))))) x))
  6. )

  7. ;;字符分割,通用函数
  8. (defun SplitA (str p / pa sl xn)
  9. (setq xn (1+ (strlen p)))
  10. (while (setq pa (vl-string-search p str))
  11.     (setq sl (cons (substr str 1 pa) sl)
  12.         str (substr str (+ pa xn)))
  13. )(cons str sl)
  14. )
发表于 2017-10-26 11:32:21 | 显示全部楼层
回帖是一种美德!感谢楼主的无私分享 谢谢
发表于 2017-10-26 11:50:41 | 显示全部楼层
Ref:

  1. (vl-load-com)
  2. (defun c:tt ()
  3.   (if (setq fp (open "V:\\bh.txt" "r"))
  4.     (progn
  5.       (setq i 1)
  6.       (while (setq txt (read-line fp))
  7.          (setq txt (vl-string-right-trim " " txt)
  8.                idx (vl-string-position (ascii ",") txt nil T)
  9.                 bm (substr txt (1+ (1+ idx)))
  10.          )
  11.          (set (read (strcat "bh" (itoa i))) bm)
  12.          (setq i (1+ i))
  13.       )
  14.       (close fp)
  15.     )
  16.   )
  17. )
发表于 2017-10-26 14:48:39 | 显示全部楼层
厉害,学习了
 楼主| 发表于 2017-10-26 17:21:43 | 显示全部楼层

学习了,谢谢
不知有没有小写改大写的通用函数
发表于 2017-10-26 18:06:52 | 显示全部楼层
Returns a string where all alphabetic characters have been converted to uppercase or lowercase

(strcase string [which])
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"

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-19 21:52 , Processed in 0.177496 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表