[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to get list of vectors with value from file content...
From: |
Xah Lee |
Subject: |
Re: how to get list of vectors with value from file content... |
Date: |
Wed, 08 Dec 2010 15:27:13 -0000 |
User-agent: |
G2/1.0 |
On Sep 2, 7:31 am, TheFlyingDutchman <zzbba...@aol.com> wrote:
> On Sep 2, 6:29 am, TheFlyingDutchman <zzbba...@aol.com> wrote:
>
> > > (1) here's how i get the file content for each file:
>
> > > (find-file "findreplace_01_A.txt" ) (setq findreplace_01_A (buffer-
> > > string)) (kill-buffer )
> > > (find-file "findreplace_01_B.txt" ) (setq findreplace_01_B (buffer-
> > > string)) (kill-buffer )
> > > (find-file "findreplace_02_A.txt" ) (setq findreplace_02_A (buffer-
> > > string)) (kill-buffer )
> > > (find-file "findreplace_02_B.txt" ) (setq findreplace_02_B (buffer-
> > > string)) (kill-buffer )
> > > (find-file "findreplace_03_A.txt" ) (setq findreplace_03_A (buffer-
> > > string)) (kill-buffer )
> > > (find-file "findreplace_03_B.txt" ) (setq findreplace_03_B (buffer-
> > > string)) (kill-buffer )
>
> > > seems a kludge to me. Is there a better way?
>
> > With find-file, if the file doesn't exist (in this case it should)
> > there is no error. This way generates an error if a file is missing:
>
> > (setq findreplace_01_A
> > (with-temp-buffer
> > (insert-file-contents "findreplace_01_A.txt")
> > (buffer-string)
> > ))
>
> A function can be created to get a file string:
>
> (defun get-file-string (fileName)
> (let ( fileString )
> (setq fileString
> (with-temp-buffer
> (insert-file-contents fileName)
> (buffer-string)))))
>
> allowing:
>
> (setq findreplace_01_A
> (get-file-string "findreplace_01_A.txt"))
>
> and a loop can be used with the function:
>
> (progn
> (defvar findReplacePairsList nil
> "A list of replacement pairs. Each element is a vector of 2 elements.
> Each element is a string, from a file content.")
>
> (let (
> ( fileNames (vector "findreplace_01_A.txt"
> "findreplace_01_B.txt"
> "findreplace_02_A.txt"
> "findreplace_02_B.txt"
> "findreplace_03_A.txt"
> "findreplace_03_B.txt"))
> (fileStrings (make-vector 6
> ""))
> (i 0 ) )
> (while (< i 6)
> (aset fileStrings i
> (get-file-string (elt fileNames i)))
> (setq i (1+ i) ) )
>
> (setq findReplacePairsList (list
> (vector (elt fileStrings 0)
> (elt fileStrings 1))
> (vector (elt fileStrings 2)
> (elt fileStrings 3))
> (vector (elt fileStrings 4)
> (elt fileStrings 5))
> ))))
Thanks a lot FlyingDutchman!
very nice cleanup.
i didn't know about “vector”, and i should've thought about with-temp-
buffer. Thanks.
Xah