newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: SHX on June 08, 2007, 10:12:36 AM

Title: write-line
Post by: SHX on June 08, 2007, 10:12:36 AM
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
Title:
Post by: Jeff on June 08, 2007, 10:46:05 AM
You can use write-buffer.  Then you can format the line however you like:


(write-buffer file-handle (format "foo barn"))
Title:
Post by: SHX on June 08, 2007, 11:14:39 AM
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.
Title:
Post by: newdep on June 08, 2007, 11:44:24 AM
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..
Title:
Post by: SHX on June 08, 2007, 11:56:49 AM
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
Title:
Post by: newdep on June 08, 2007, 12:02:19 PM
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)

)
Title:
Post by: SHX on June 08, 2007, 12:10:06 PM
   
(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.
Title:
Post by: newdep on June 08, 2007, 12:15:58 PM
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..
Title:
Post by: SHX on June 08, 2007, 12:21:05 PM
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.
Title:
Post by: newdep on June 08, 2007, 12:24:43 PM
Aaaaaaaaaaaaa now i get it ;-)



oke here is the fix...





 (if (>= (+ 1 $idx) (length list)) (write-file or append-file...;; not write-line!
Title:
Post by: SHX on June 08, 2007, 12:34:23 PM
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.
Title:
Post by: newdep on June 08, 2007, 12:36:07 PM
That is indeed someting different ..Enjoy ;-)
Title:
Post by: Jeff on June 08, 2007, 01:33:22 PM
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"))
Title:
Post by: cormullion on June 08, 2007, 02:52:59 PM
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))
Title:
Post by: Jeff on June 08, 2007, 03:18:14 PM
join will put a carriage return only between items in the list.
Title:
Post by: cormullion on June 08, 2007, 03:40:41 PM
oops - confused your code with Lutz's ... :-) Sorry jeff! I was really replying to the 8:21 pm post, but I got sidetracked...
Title:
Post by: SHX on June 10, 2007, 06:02:30 AM
Jeff,



Thanks, exactly what I was looking for.



Steven