write-line

Started by SHX, June 08, 2007, 10:12:36 AM

Previous topic - Next topic

SHX

I am using "write-line" to write to a file. The problem is that it always append "rn" carriage return and new line to each line. How can I not have that happen (or clean it up) on the last line?



Steven

Jeff

#1
You can use write-buffer.  Then you can format the line however you like:


(write-buffer file-handle (format "foo barn"))
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

SHX

#2
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.

newdep

#3
I dont realy get it yet ;-) but ill try..



If you are appening the "rn" then use write-file/write-buffer instead of write-line



if you data already has 'rn" and want to remove it use  replace, chop or regex

or filter..etc..
-- (define? (Cornflakes))

SHX

#4
newdep thanks,



here is the general flow of the code
   (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 FilesMicrosoft OfficeOffice10EXCEL.EXE

c:Program FilesMicrosoft OfficeOffice10winword.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

newdep

#5
like this?





(setq lines '( "this is line onern" "rnthis is another line"))

(dolist (x lines)

  (replace "rn" x "")

  (append-file "mine" x)  ;; or (write-line "mine" x)

)
-- (define? (Cornflakes))

SHX

#6
   
(setq thelistbeingprocessed '("c:Program FilesMicrosoft OfficeOffice10EXCEL.EXE" "c:Program FilesMicrosoft OfficeOffice10winword.exe" c:Program FilesMicrosoft OfficeOffice10xxxx.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 "rn" is not currently in the list only getting there by writing it out.

newdep

#7
I still have troubles understanding the issue.....sorry :-)



But if the list doesn't contain any rn 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..
-- (define? (Cornflakes))

SHX

#8
That is what i am doing.



And the write-line puts "rn" at the end of each line which is good until it writes the last line which should not have the "rn"



Thanks again for your help.

newdep

#9
Aaaaaaaaaaaaa now i get it ;-)



oke here is the fix...





 (if (>= (+ 1 $idx) (length list)) (write-file or append-file...;; not write-line!
-- (define? (Cornflakes))

SHX

#10
newdep, thanks again but I guess I am not to clear.



The solution that I found is
(write-file "myfile" (chop (read-file "myfile") 2) )

This will cut off the final "rn" and rewite the file.

newdep

#11
That is indeed someting different ..Enjoy ;-)
-- (define? (Cornflakes))

Jeff

#12
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 rn to the file.



An easier, more elegant solution would be:


(write-buffer out-file
  (join (filter file? thelistbeingprocessed) "rn"))
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

cormullion

#13
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:


(print (first the-list))
(dolist (i (rest the-list))
   (print "rn" i))

Jeff

#14
join will put a carriage return only between items in the list.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code