Writing a list to txt-file

Writing a list to txt-file

thomas.schive
Enthusiast Enthusiast
2,522 Views
3 Replies
Message 1 of 4

Writing a list to txt-file

thomas.schive
Enthusiast
Enthusiast

Hello!

 

So - I have a list made by appending a wild card conditions from another:

(foreach x ListA

(if  (wcmatch x "111*")

(setq ListB (append (ListA (list x)))

))

 

However, I would like my ListB to be written to a txt file! I have experimented with:

(mapcar (function (lambda ...

to place (princ)'s in order to create space shifts in a txt document. 

... But it does not seem to work properly ... 

 

Is there a way to take ListB's content and write it to a txt-file?

0 Likes
Accepted solutions (1)
2,523 Views
3 Replies
Replies (3)
Message 2 of 4

john.uhden
Mentor
Mentor

Try (vl-princ-to-string B)

Though you will lose some precision of any reals in the list(s).

John F. Uhden

0 Likes
Message 3 of 4

CodeDing
Advisor
Advisor
Accepted solution

@thomas.schive ,

 

You could write your entire list to the text file with vl-prin1-to-string then use the READ function to store it as a variable again. Is this what you're looking for?

 

.....
(setq fName "c:\\users\\myName\\myFolder\\myFile.txt")
(setq f (open fName "w"))
(write-line (vl-prin1-to-string ListB) f)
(close f)
.....
(setq f (open fName "r"))
(setq str (read-line f))
(close f)
(setq ListB (read str))
.....

 

Best,

~DD

0 Likes
Message 4 of 4

ВeekeeCZ
Consultant
Consultant

What do you mean by "space shift"?

Would you give us an example of lista and desired txt file outcome?

0 Likes