Page 1 of 1
write-line
Posted: Fri Jun 08, 2007 5:12 pm
by SHX
I am using "write-line" to write to a file. The problem is that it always append "\r\n" carriage return and new line to each line. How can I not have that happen (or clean it up) on the last line?
Steven
Posted: Fri Jun 08, 2007 5:46 pm
by Jeff
You can use write-buffer. Then you can format the line however you like:
Code: Select all
(write-buffer file-handle (format "foo bar\n"))
Posted: Fri Jun 08, 2007 6:14 pm
by SHX
Jeff, thanks for your help.
I am not sure how that will help.
I am processing a list and depending upon certain values writing items of that list to a file.
so for each item on the list that meets certain criteria, I am writing that item as a line in a file.
Therefore I always need a carriage return either at the end of the line or the begining of the line.
If it is at the end of the line then there will be an extra line at the end and if it is in the begining there will be an extra liner in the begininng.
I am probably missing something obvious.
Posted: Fri Jun 08, 2007 6:44 pm
by newdep
I dont realy get it yet ;-) but ill try..
If you are appening the "\r\n" then use write-file/write-buffer instead of write-line
if you data already has '\r\n" and want to remove it use replace, chop or regex
or filter..etc..
Posted: Fri Jun 08, 2007 6:56 pm
by SHX
newdep thanks,
here is the general flow of the code
Code: Select all
(dolist (x thelistbeingprocessed )
(if (file? x) (write-line x out-file) )
process a list of files that if they exist then write them out into a file.
So if there are only 2 items on the list that exist on the computer it the new file should have 2 lines each having the name of a file that exists like
c:\Program Files\Microsoft Office\Office10\EXCEL.EXE
c:\Program Files\Microsoft Office\Office10\winword.exe
without a blank line in front or back.
I am new at this so the answer might be obvious.
But what i am asking is, is there a straight forward way while writing to a file while processing a list to eliminate the extra line , or do you have to clean it up once the file writing is completed.
Sorry if it is confusing
Posted: Fri Jun 08, 2007 7:02 pm
by newdep
like this?
(setq lines '( "this is line one\r\n" "\r\nthis is another line"))
(dolist (x lines)
(replace "\r\n" x "")
(append-file "mine" x) ;; or (write-line "mine" x)
)
Posted: Fri Jun 08, 2007 7:10 pm
by SHX
Code: Select all
(setq thelistbeingprocessed '("c:\Program Files\Microsoft Office\Office10\EXCEL.EXE" "c:\Program Files\Microsoft Office\Office10\winword.exe" c:\Program Files\Microsoft Office\Office10\xxxx.EXE)
(dolist (x thelistbeingprocessed )
(if (file? x) (write-line x out-file) )
So if an item on the list exists write out the file name with each file name being a new line.
So the "\r\n" is not currently in the list only getting there by writing it out.
Posted: Fri Jun 08, 2007 7:15 pm
by newdep
I still have troubles understanding the issue.....sorry :-)
But if the list doesn't contain any \r\n and you are using 'write-line
then do only write if the 'x is not empty!
(dolist (x list)
(if (not (empty? x)) (write-line "mine" x)))
PS: you might use a 'trim to clean up white spaces befor the empty check..
Posted: Fri Jun 08, 2007 7:21 pm
by SHX
That is what i am doing.
And the write-line puts "\r\n" at the end of each line which is good until it writes the last line which should not have the "\r\n"
Thanks again for your help.
Posted: Fri Jun 08, 2007 7:24 pm
by newdep
Aaaaaaaaaaaaa now i get it ;-)
oke here is the fix...
(if (>= (+ 1 $idx) (length list)) (write-file or append-file...;; not write-line!
Posted: Fri Jun 08, 2007 7:34 pm
by SHX
newdep, thanks again but I guess I am not to clear.
The solution that I found is
Code: Select all
(write-file "myfile" (chop (read-file "myfile") 2) )
This will cut off the final "\r\n" and rewite the file.
Posted: Fri Jun 08, 2007 7:36 pm
by newdep
That is indeed someting different ..Enjoy ;-)
Posted: Fri Jun 08, 2007 8:33 pm
by Jeff
newdep was right. The $idx variable stores the current iterations. If you check that the current iteration is less than the length of the list, you effectively do not write the extra \r\n to the file.
An easier, more elegant solution would be:
Code: Select all
(write-buffer out-file
(join (filter file? thelistbeingprocessed) "\r\n"))
Posted: Fri Jun 08, 2007 9:52 pm
by cormullion
Lutz usually proposes the definitive solution. But I thought - when I first saw this thread - that you needed to apply a bit of lateral thinking to the algorithm - put the line breaks first. A bit like this:
Code: Select all
(print (first the-list))
(dolist (i (rest the-list))
(print "\r\n" i))
Posted: Fri Jun 08, 2007 10:18 pm
by Jeff
join will put a carriage return only between items in the list.
Posted: Fri Jun 08, 2007 10:40 pm
by cormullion
oops - confused your code with Lutz's ... :-) Sorry jeff! I was really replying to the 8:21 pm post, but I got sidetracked...
Posted: Sun Jun 10, 2007 1:02 pm
by SHX
Jeff,
Thanks, exactly what I was looking for.
Steven