MarcoW 发表于 2022-7-6 09:45:38

Lisp从外部文件读取

今天就是这样的一天。。。我找不到问题。
 
我的目标:
读取外部文件并获取“列表列表”。
 
我的Lisp程序:
 
(defun Test (/ MyFile MyOpenFile Line AllMyFile)
(setq MyFile "C:\\MyExternalFile.txt")
(if MyFile
   (progn
   (setq MyOpenFile (open MyFile "r"))
   (setq AllMyFile nil)
   (while
(setq Line (read (read-line MyOpenFile)))
(setq AllMyFile (append AllMyFile (list Line)))
   )
   (close MyOpenFile)
   ) ;_progn
   (alert "No external file found !!")
)
(princ)
) ;_defun
 
我的外部文件:

("aName" "anAdress1" "SomeOtherData1")
("bName" "anAdress2" "SomeOtherData2")
("cName" "anAdress3" "SomeOtherData3")
("dName" "anAdress4" "SomeOtherData4")


 
分析:

I want to get this:
(("aName" "anAdress1" "SomeOtherData1") ("bName" "anAdress2" "SomeOtherData2") ("cName" "anAdress3" "SomeOtherData3") ("dName" "anAdress4" "SomeOtherData4"))


 
但它返回的是:

Command: (test)
; error: bad argument type: stringp nil


 
我知道这是因为它试图读取一个空行,但我认为它只会在该行不是零的情况下读取该行。
因此错误为“stringp nil”。
 
但话说回来,我做错了什么?这一定是个小问题。旁注:不知怎的,我怀疑这个部分”(读(读行…)因为省略第一个“read”确实有效。然而,它返回了一个充满“的列表,如果不进行解析,我将无法使用它”。
 
一如既往,我们非常感谢您的帮助。

Lt Dan's l 发表于 2022-7-6 09:57:14

------外部文件从这里开始---------
1.
“测试”
“测试1”
 
2.
“测试2”
“测试3”
--------到此结束---------
 
(_读取“文件位置”)
将返回
((1(“testing”“testing1”))(2(“testing2”“testing3”))
 
 
(defun _read ( filename / fname line lst temp newlist )
(if (findfile filename)
   (progn
   (setq fname (open filename "r"))
   (while (setq line (read-line fname))
       (if (not (wcmatch line ""))
         (setq lst (append (list line) lst))
       )
   )
   (setq fname (close fname))
   (if lst
       (foreach x (mapcar 'read lst)

         ;|Big thanks to Alanjt|;
         (if (numberp x)
         (setq temp (not (setq newlist (cons (list x temp) newlist))))
         (setq temp (cons x temp))
         )


       )
   )
   (if newlist
       newlist
       (mapcar 'read lst)
   )
   )
)
)

MarcoW 发表于 2022-7-6 09:59:09

谢谢你的回复,丹中尉的腿,但我不知道你的意思
此外,我想了解为什么我的lisp在没有使用完全不同的lisp的情况下无法工作。
除非我的Lisp程序是完全错误的,当然。。。

Lt Dan's l 发表于 2022-7-6 10:05:27


(defun _Test ( MyFile / MyOpenFile Line AllMyFile)
(if (findfile MyFile)
   (progn
   (setq MyOpenFile (open MyFile "r"))
   (while
       (setq Line (read-line MyOpenFile))
       (if (not (wcmatch line ""))
         (setq AllMyFile (append AllMyFile (list line)))
       )
   )
   (close MyOpenFile)
   )
)
AllMyFile
)

 
尝试
(\u Test“C:\\MyExternalFile.txt”)

MarcoW 发表于 2022-7-6 10:11:05

我尝试了这个例程,它返回以下内容:
 

("(\"dName\" \"anAdress4\"
\"SomeOtherData4\")" "(\"cName\" \"anAdress3\" \"SomeOtherData3\")" "(\"bName\"
\"anAdress2\" \"SomeOtherData2\")" "(\"aName\" \"anAdress1\"
\"SomeOtherData1\")")

 
应该是这样的:
 

(("dName" "anAdress4" "SomeOtherData4") ("cName" "anAdress3" "SomeOtherData3")("bName" "anAdress2" "SomeOtherData2") ("aName" "anAdress1" "SomeOtherData1"))

 
使用额外的“read”进行了尝试,但仍然出现以下错误:

; error: bad argument type: stringp nil

 
基本上代码可以工作,但当它到达外部文件中内容的末尾时,它崩溃了。代码
(read(read-line MyOpenFile)))无法处理nil或“”。。。。
 
即使它处于一个while循环中,也会崩溃。很明显,我错过了一件重要的事情,但是什么??

Lt Dan's l 发表于 2022-7-6 10:16:34

抱歉,这么长时间才回复
 
 
read无法处理nil
简单测试。尝试
(读取零)
 

(defun _Test ( MyFile / MyOpenFile Line AllMyFile newlist temp)
(if (findfile MyFile)
   (progn
   (setq MyOpenFile (open MyFile "r"))
   (while
       (setq Line (read-line MyOpenFile))
       (if (not (wcmatch line ""))
         (setq AllMyFile (append (list line) AllMyFile ))
       )
   )
   (close MyOpenFile)
   (foreach x allmyfile
       (if (wcmatch (strcase x) "*NAME*")
         (setq temp (not (setq newlist (cons (cons x temp) newlist))))
         (setq temp (cons x temp))
       )
   )
   )
)
(if newlist newlist allmyfile)
)

 
(\u Test“C:\\MyExternalFile.txt”)

MarcoW 发表于 2022-7-6 10:27:10

没问题!目前我无法访问autocad,但我会尽快尝试代码。
谢谢你抽出时间。
 
通过电话发送的消息,抱歉出错。

alanjt 发表于 2022-7-6 10:33:14

(defun _read (file / r l)
(if (and (findfile file) (setq file (open file "R")))
   (progn (while (setq r (read-line file)) (setq l (cons (read r) l))) (close file) (reverse l))
)
)

alanjt 发表于 2022-7-6 10:38:42

顺便说一句,(append(list))比(cons)慢得多,最后使用reverse。

MarcoW 发表于 2022-7-6 10:42:05

@丹的腿:
谢谢你的努力,我现在可以理解这个问题了,确实“read”不能处理nil,用(read nil)测试。从一开始,我对“阅读”有一些奇怪的感觉,但它带走了“符号”。太糟糕了,因为我离得很近,所以我自己无法到达那里。
 
 
@Alanjt:
谢谢你的代码,我不想使用其他方法,因为我想知道我的代码出了什么问题。但是在阅读了你的代码后,我惊讶地发现你的代码有多么短,多么容易理解。
 
 
我一直在读这个和这个,发现两者有些相同。至少如果你读了它,它看起来是一样的。那么为什么你说使用Cons比Append更快呢?当然,我毫不怀疑你是错的:-)
 
无论如何,我现在可以继续了。
谢谢你们俩!
页: [1] 2
查看完整版本: Lisp从外部文件读取