ynotrobits 发表于 2022-7-5 22:51:56

Use read-line to extract comma

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 arectangle)
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

Tharwat 发表于 2022-7-5 22:57:06

Welcome to forum .
 
E.g.
 

(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 25.50 32.00 MyRoomName PL1"

Lee Mac 发表于 2022-7-5 22:59:26

Consider the following functions:
 
http://lee-mac.com/stringtolist.html
 
http://lee-mac.com/readcsv.html

ynotrobits 发表于 2022-7-5 23:01:58

-----Thank you

jmerch 发表于 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:

(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

BIGAL 发表于 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.

Tharwat 发表于 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 .
 

(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 .
 

(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 .

jmerch 发表于 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)
 

(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.

Tharwat 发表于 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 .

jmerch 发表于 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!
页: [1] 2
查看完整版本: Use read-line to extract comma