thebiglebowski Posted August 14, 2019 Share Posted August 14, 2019 I am trying to read in "dotted pair" lists from a text file, but am having trouble getting it to work. if I simply copy and paste in my dotted pair list to the command line it works fine. Example (setq lst '((TYPE . 4 ) (TOOL . 2) (DIA . 0.750))) Works fine , including the assoc functions but, if I try to do this via read-line it does not create the dotted pair list as I want it to. I am sure I am missing something simple but I do not know what. Any input would be appreciated. Read line code (setq f (open "c:\\ACAD_SETUP\\LSP\\TOOL.LIB" "r")) (setq toolinfo (read-line f)) dotted pairs as from text file '((TYPE . 4 ) (TOOLNO . 1) (DIA . 0.500) (DM . 0.950) (CHIPLOAD . 0.002) (SFM . 250.0) (NOTEETH . 4.0) (DOC . 0.030) (COOLANT . COOLON) (COMMENT . "0.500 4 FLUTE ENDMILL")) '((TYPE . 4 ) (TOOLNO . 2) (DIA . 0.750) (DM . 1.500) (CHIPLOAD . 0.0015) (SFM . 250.0) (NOTEETH . 4.0) (DOC . 0.030) (COOLANT . COOLON) (COMMENT . "0.750 4 FLUTE ENDMILL")) Quote Link to comment Share on other sites More sharing options...
myloveflyer Posted August 14, 2019 Share Posted August 14, 2019 36 minutes ago, thebiglebowski said: 我正试图从文本文件中读取“虚线对”列表,但在使其工作时遇到了困难。 如果我只是简单地将虚线对列表复制并粘贴到命令行,它就能正常工作。 示例(setqlst‘)(类型)。4)(工具。2)(DIA.(0.750)工作正常,包括Assoc功能 但是,如果我试图通过读行来实现这一点,它不会像我想要的那样创建虚线对列表。我肯定我错过了一些简单的东西,但我不知道是什么。如有任何意见,将不胜感激。 读行码 (setq f (open "c:\\ACAD_SETUP\\LSP\\TOOL.LIB" "r")) (setq toolinfo (read-line f)) 从文本文件中的虚线对 '((TYPE . 4 ) (TOOLNO . 1) (DIA . 0.500) (DM . 0.950) (CHIPLOAD . 0.002) (SFM . 250.0) (NOTEETH . 4.0) (DOC . 0.030) (COOLANT . COOLON) (COMMENT . "0.500 4 FLUTE ENDMILL")) '((TYPE . 4 ) (TOOLNO . 2) (DIA . 0.750) (DM . 1.500) (CHIPLOAD . 0.0015) (SFM . 250.0) (NOTEETH . 4.0) (DOC . 0.030) (COOLANT . COOLON) (COMMENT . "0.750 4 FLUTE ENDMILL")) Please provide the read text or make a CSV Quote Link to comment Share on other sites More sharing options...
BIGAL Posted August 14, 2019 Share Posted August 14, 2019 (edited) Try something like this, it seemed to work testing (setq ans " '((TYPE . 4 ) (TOOLNO . 1) (DIA . 0.500) (DM . 0.950) (CHIPLOAD . 0.002))") the read line would return a string. I am sure others will post if this is not the correct way. (setq toolinfo (eval (read (read-line f)))) Edited August 14, 2019 by BIGAL Quote Link to comment Share on other sites More sharing options...
thebiglebowski Posted August 14, 2019 Author Share Posted August 14, 2019 40 minutes ago, BIGAL said: Try something like this, it seemed to work testing (setq ans " '((TYPE . 4 ) (TOOLNO . 1) (DIA . 0.500) (DM . 0.950) (CHIPLOAD . 0.002))") the read line would return a string. I am sure others will post if this is not the correct way. (setq toolinfo (eval (read (read-line f)))) This appears to work. whether or not it is the correct way to do it Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 14, 2019 Share Posted August 14, 2019 The reason that your original code does not behave like an association list is because the AutoLISP read-line function will return a string representing a text line read from a file; therefore, your original expression: (setq toolinfo (read-line f)) Is essentially equivalent to a literal string assignment: (setq toolinfo "'((TYPE . 4 ) (TOOLNO . 1) ... ") The key to converting the string to the AutoLISP data it contains is the read function, which returns the first list (or atom) which may be obtained from a supplied string, observe: _$ (setq mystring "(1 2 3)") "(1 2 3)" _$ (type mystring) STR _$ (setq mylist (read mystring)) (1 2 3) _$ (type mylist) LIST _$ (car mylist) 1 The reason that you are also needing to use the eval function is because your string contains a quoted list: _$ (setq mystring "'(1 2 3)") "'(1 2 3)" _$ (type mystring) STR _$ (setq mylist (read mystring)) (QUOTE (1 2 3)) _$ (type mylist) LIST _$ (setq mylist (eval mylist)) (1 2 3) _$ (type mylist) LIST _$ (car mylist) 1 Quote Link to comment Share on other sites More sharing options...
myloveflyer Posted August 16, 2019 Share Posted August 16, 2019 On 8/14/2019 at 8:28 PM, Lee Mac said: 原始代码不像关联列表的原因是AutoLISP读行函数将返回一个字符串,表示从文件中读取的文本行;因此,您的原始表达式: (setq toolinfo (read-line f)) 实质上等同于文字字符串赋值: (setq toolinfo "'((TYPE . 4 ) (TOOLNO . 1) ... ") 将字符串转换为它包含的AutoLISP数据的键是朗读,阅读函数,它返回可以从提供的字符串获得的第一个列表(或原子),它注意到: _$ (setq mystring "(1 2 3)") "(1 2 3)" _$ (type mystring) STR _$ (setq mylist (read mystring)) (1 2 3) _$ (type mylist) LIST _$ (car mylist) 1 您还需要使用埃瓦尔函数是因为您的字符串包含一个引号列表: _$ (setq mystring "'(1 2 3)") "'(1 2 3)" _$ (type mystring) STR _$ (setq mylist (read mystring)) (QUOTE (1 2 3)) _$ (type mylist) LIST _$ (setq mylist (eval mylist)) (1 2 3) _$ (type mylist) LIST _$ (car mylist) 1 Lee,Every time you can explain the problem clearly! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 16, 2019 Share Posted August 16, 2019 10 hours ago, myloveflyer said: Lee,Every time you can explain the problem clearly! Thanks - I try my best Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.