unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to get list of vectors with value from file content...
@ 2010-09-02 10:16 Xah Lee
  2010-09-02 11:15 ` Marc Mientki
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Xah Lee @ 2010-09-02 10:16 UTC (permalink / raw)
  To: help-gnu-emacs

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/^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 10:16 how to get list of vectors with value from file content Xah Lee
@ 2010-09-02 11:15 ` Marc Mientki
  2010-09-02 21:15   ` Xah Lee
  2010-09-02 12:42 ` TheFlyingDutchman
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Marc Mientki @ 2010-09-02 11:15 UTC (permalink / raw)
  To: help-gnu-emacs

Am 02.09.2010 12:16, schrieb Xah Lee:
> 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.
[...]
> (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.

Why vectors? Why not alist (pairs)?

regards
Marc



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 10:16 how to get list of vectors with value from file content Xah Lee
  2010-09-02 11:15 ` Marc Mientki
@ 2010-09-02 12:42 ` TheFlyingDutchman
  2010-09-02 13:00 ` TheFlyingDutchman
  2010-09-02 13:29 ` TheFlyingDutchman
  3 siblings, 0 replies; 8+ messages in thread
From: TheFlyingDutchman @ 2010-09-02 12:42 UTC (permalink / raw)
  To: help-gnu-emacs


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

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

The vector function allows vectors to be created with symbol values.
So you can replace the above with:

  (setq fr1 (vector findreplace_01_A findreplace_01_B))
  (setq fr2 (vector findreplace_02_A findreplace_02_B))
  (setq fr3 (vector findreplace_03_A findreplace_03_B))


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 10:16 how to get list of vectors with value from file content Xah Lee
  2010-09-02 11:15 ` Marc Mientki
  2010-09-02 12:42 ` TheFlyingDutchman
@ 2010-09-02 13:00 ` TheFlyingDutchman
  2010-09-02 13:29 ` TheFlyingDutchman
  3 siblings, 0 replies; 8+ messages in thread
From: TheFlyingDutchman @ 2010-09-02 13:00 UTC (permalink / raw)
  To: help-gnu-emacs


> 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)
>        ))

Forgot to add that the vector function works here as well:

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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 10:16 how to get list of vectors with value from file content Xah Lee
                   ` (2 preceding siblings ...)
  2010-09-02 13:00 ` TheFlyingDutchman
@ 2010-09-02 13:29 ` TheFlyingDutchman
  2010-09-02 14:31   ` TheFlyingDutchman
  3 siblings, 1 reply; 8+ messages in thread
From: TheFlyingDutchman @ 2010-09-02 13:29 UTC (permalink / raw)
  To: help-gnu-emacs


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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 13:29 ` TheFlyingDutchman
@ 2010-09-02 14:31   ` TheFlyingDutchman
  2010-09-02 21:02     ` Xah Lee
  0 siblings, 1 reply; 8+ messages in thread
From: TheFlyingDutchman @ 2010-09-02 14:31 UTC (permalink / raw)
  To: help-gnu-emacs

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))
                              ))))



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 14:31   ` TheFlyingDutchman
@ 2010-09-02 21:02     ` Xah Lee
  0 siblings, 0 replies; 8+ messages in thread
From: Xah Lee @ 2010-09-02 21:02 UTC (permalink / raw)
  To: help-gnu-emacs


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: how to get list of vectors with value from file content...
  2010-09-02 11:15 ` Marc Mientki
@ 2010-09-02 21:15   ` Xah Lee
  0 siblings, 0 replies; 8+ messages in thread
From: Xah Lee @ 2010-09-02 21:15 UTC (permalink / raw)
  To: help-gnu-emacs

On Sep 2, 4:15 am, Marc Mientki <mien...@nonet.com> wrote:
> Am 02.09.2010 12:16, schrieb Xah Lee:
>
> > 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.
> [...]
> > (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.
>
> Why vectors? Why not alist (pairs)?

humm... i think that might work too. This myPairList  is fed to a
function i wrote that do find/replace pairs. When i wrote that
function, i don't remember whether the i made input to accept a alist
as well...

 Xah


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-09-02 21:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-02 10:16 how to get list of vectors with value from file content Xah Lee
2010-09-02 11:15 ` Marc Mientki
2010-09-02 21:15   ` Xah Lee
2010-09-02 12:42 ` TheFlyingDutchman
2010-09-02 13:00 ` TheFlyingDutchman
2010-09-02 13:29 ` TheFlyingDutchman
2010-09-02 14:31   ` TheFlyingDutchman
2010-09-02 21:02     ` Xah Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).