乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 29|回复: 19

[编程交流] Use read-line to extract comma

[复制链接]

4

主题

11

帖子

7

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-5 22:51:56 | 显示全部楼层 |阅读模式
Hello all,
 
I've recently come back to AutoCad after a few years absence and I'm surprised at how Lisp I've forgotten. I used to know how to do this:
 
I can't remember how to extract the comma separated values from a text string. If I use the following read-line formula:
 
(setq ReadText (open "C:\\MyFolder\\MyText" "r"))
 
(setq UseText(read-line ReadText))
 
(Close ReadText)
 
Which extracts the following text:
 
 
1,25.50,32.00,MyRoomName,PL1
2,25.50,46.00,MyRoomName,PL1
3,25.50,72.00,MyRoomName,PL12
 
What I can't remember how to do is isolate and assign the comma separated values to new variables.
 
As an FYI, in this particular case the value order is as follows from left to right:
 
1-----------------Object number (Object is always a  rectangle)
25.50-------------Object width
32.00-------------Object Length
MyRoomName------Room name in which object is located
L1----------------Object material code
 
In other words, how do I break down the UseText variable into its component parts and then generate the five independent variables that I need to create additional formulas?
 
Thanks for looking
 
AJ
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 22:57:06 | 显示全部楼层
Welcome to forum .
 
E.g.
 
  1. (setq a "1,25.50,32.00,MyRoomName,PL1")(while (if (setq n (vl-string-search "," a 0))   (setq a (vl-string-subst " " "," a)) ))
 
Result ...
 
  1. "1 25.50 32.00 MyRoomName PL1"
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 22:59:26 | 显示全部楼层
Consider the following functions:
 
http://lee-mac.com/stringtolist.html
 
http://lee-mac.com/readcsv.html
回复

使用道具 举报

4

主题

11

帖子

7

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-5 23:01:58 | 显示全部楼层
-----Thank you
回复

使用道具 举报

20

主题

95

帖子

75

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-5 23:05:21 | 显示全部楼层
 
Tharwat, I am having a similar issue but am not sure where to insert your code.  Once my file is open, here is my code:
  1. (read-line f)(while (setq row (read-line f))   (mapfilter (strcat "#5113 = " row) sset)   (executescript **SCRIPT HERE**)   (princ))(close f)
 
the "mapfilter" is a third party command. I need it to read my excel sheet which will have 2 columns. I just need it to read the first column and for each value, do the third party commands.
 
I can change the first 'while' to (while (setq row (list (read-line f))) but then I do not know where to remove the comma's in order for the 'mapfilter' to grab the data it needs and continue in the while loop.
 
TIA
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 23:10:04 | 显示全部楼层
Jmerch go back and read the posts by Lee & Tharwat they have both provided the solutions to the questions. If you use the to list option Readcsv.lsp then its easy to get the 1st value its just (nth 0 lst) this would be column 1.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 23:12:53 | 显示全部楼层
Jmerch .
 
I don't know what that third party command do , but to separate a string as shown in my previous example and after reading an object file .
 
  1. (setq f (open f "r"))(while (setq str (read-line f)) (while (if (setq n (vl-string-search "," str 0))          (setq str (vl-string-subst " " "," str))        ) ))(close f)
 
And to get only the first string before a comma .
 
  1. (setq f (open f "r"))(while (setq str (read-line f)) (if (setq p (vl-string-search "," str 0))   (setq str (substr str 1 p)) ))(close f)
 
Let me know if that is what you're after .
回复

使用道具 举报

20

主题

95

帖子

75

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-5 23:15:51 | 显示全部楼层
@BIGAL, I don't see anything referencing (nth 0 lst) in this post. I am looking into it though.
 
@Tharwat, When I enter your second code, it partially works. In order to test, here's how I modified it (using alert to show me what data it's reading from the csv file)
 
  1. (while (setq str (read-line f))   (if (setq p (vl-string-search "," str 0))       (setq str (substr str 1 p))   )   (alert str)   (princ))(close f)
 
This works and shows me each line it's reading. However, when I substitute my original code with mapfilter it does not work this way. Basically the data I'm reading from the csv is similar to an attribute and 'mapfilter' is like the ACAD filter. I'm wanting this to look at each cell in the first column and filter out any items with that attribute, if it's present, run a script (another third party command). Continue doing this until the list is done. So, when I have the 'alert' in the code, it's reading each line in the csv. So then I substitute that part with the 'mapfilter' and use strcat to compile what it filters for. But it doesn't work that way.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 23:19:31 | 显示全部楼层
I can not tell anything about your functions or third party commands since that I have no idea about how it works and what does these commands do for you .
回复

使用道具 举报

20

主题

95

帖子

75

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-5 23:21:33 | 显示全部楼层
I understand that. Think of it as a filter. If the data I was reading from the csv was a layer name, and using the 'alert' test goes through each layer listed in the csv file, why wouldn't filtering each layer work? It's not the third party functions that are holding it up, this works fine if my csv file just had 1 column of data and I didn't do the string-search. That's what I'm trying to figure out and didn't know if you see something I misplaced?
 
Thanks!
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-11 03:35 , Processed in 1.518729 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表