2010-09-02

elisp how to get eval vector elements

a pure academic question on emacs lisp.

basically, i want to set a variable who's value is a list of vectors, each vector is a pair.
Like this form:

(setq findReplacePairsList (list [A B] [A B] [A B]) )

the element of the vector are strings, each are read from a file content.
(i have 6 files, corresponds to the 3 vectors above.)

i have 2 questions.

(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?

(2) elisp vector elements are not evaluated. So, i cannot simply write

(setq myPairList (list [A B] [A B] [A B]) )

where the A B are already variables.

i need to use aset.

Is there a better way to do what i want?

Here's the current form of my code, does the job but seems very silly. Huge number of temp vars and intermeditate steps.

--------------------------------------------------

(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 (
findreplace_01_A
findreplace_01_B
findreplace_02_A
findreplace_02_B
findreplace_03_A
findreplace_03_B
fr1
fr2
fr3
)
(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 )

(setq fr1 ["dummy" "dummy"] )
(setq fr2 ["dummy" "dummy"] )
(setq fr3 ["dummy" "dummy"] )

(aset fr1 0 findreplace_01_A)
(aset fr1 1 findreplace_01_B)
(aset fr2 0 findreplace_02_A)
(aset fr2 1 findreplace_02_B)
(aset fr3 0 findreplace_03_A)
(aset fr3 1 findreplace_03_B)

(setq findReplacePairsList (list
fr1
fr2
fr3
))
)

thanks.

seems my second problem is solved if i have list of list instead of list of vectors. With list of list, i can simply write

(setq findReplacePairsList
(list
(list findreplace_01_A findreplace_01_B)
(list findreplace_02_A findreplace_02_B)
(list findreplace_03_A findreplace_03_B)
))

Xah ∑ http://xahlee.org/ ☄

1 comment:

  1. On Sep 2, 4:10 am, p...@informatimago.com (Pascal J. Bourguignon) wrote:
    > No, it's not the Xah Lee, it's the mini-Xah Lee who's asking.
    >
    > (defun file-contents (path)
    >   (with-temp-buffer
    >     (insert-file-contents path)
    >     (buffer-string)))
    >
    > (defun get-pairs-of-file-contents (pairs-of-files)
    >   (mapcar (lambda (pair-of-file)
    >              (map 'vector (function file-contents) pair-of-file))
    >           pairs-of-files))
    >
    > (get-pairs-of-file-contents '(("/tmp/a1" "/tmp/a2")
    >                               ("/tmp/b1" "/tmp/b2")
    >                               ("/tmp/c1" "/tmp/c2")))

    thanks! That's fantastic, especially the get-pairs-of-file-contents part.



    > > (2) elisp vector elements are not evaluated. So, i cannot simply write
    >
    > > (setq myPairList (list [A B] [A B] [A B]) )
    >
    > > where the A B are already variables.
    >
    > > i need to use aset.
    >
    > Or you could use vector, like you use list.

    thanks to Pascal J. Bourguignon and TheFlyingDutchman.

    ReplyDelete