[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Writing to buffer/file
From: |
Michael Powe |
Subject: |
Writing to buffer/file |
Date: |
Wed, 08 Dec 2010 15:28:26 -0000 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.3 (gnu/linux) |
Hello,
I am writing a function in elisp to do the following:
open and read an .ini file
search the file for header lines of format [someheader]
put header value as key of hash
continue to search for string value (e.g., "profilename=.*")
add found string value to list which is set as value for the header key
for each header, a small number of items will be added to the list
after reading through ini file and filling the hash:
open another buffer and write the contents of the hash to the buffer
write the buffer to file
Here is my function:
(defun extract-ini-settings ()
(interactive)
(let (myhash mybuff myfile header strmatch results output strvalues mylist)
(setq myfile "c:\\share\\reports_default.ini")
(setq myhash (make-hash-table :test 'equal))
(setq mybuff (find-file myfile))
(set-buffer mybuff)
(setq strvalues "")
(while
(re-search-forward
"\\[customtableprofile[0-9]+\\]"
nil t)
(when (match-string 0)
(setq header (match-string 0))
(puthash header '() myhash)
(while
(re-search-forward
"profilename=.*" nil t)
(when (match-string 0)
(setq strmatch (match-string 0))
(puthash header (cons strmatch (gethash header myhash))
myhash)))))
(kill-buffer mybuff)
(message (number-to-string (hash-table-count myhash)))
(setq output "c:\\share\\reports_headers.txt")
(setq results (find-file output))
(set-buffer results)
(point-min)
;; this works, i.e. writes the string to the `results' buffer
;; and each time I run the function, a new copy of the string
;; is appended to the buffer.
(insert "Start of file\n")
(point-max)
(hash-to-list (myhash mylist))
(insert mylist)
))
Here is the helper function hash-to-list:
(defun hash-to-list (hashtable mylist)
"Return a list that represent the hashtable."
(maphash (lambda (kk vv) (setq mylist (cons (list kk vv) mylist)))
hashtable)
mylist)
I am evidently grossly misunderstanding the mechanism for writing to
file. The (message ...) to write the hash count to *Messages* writes
112, which is the correct count of header lines for my test file. When
I inserted a (message ...) in the loop for capturing the headers, they
were all written as expected to the *Messages* buffer.
I don't know if I'm a victim of scope rules, or my data capture is f'ed
up. Or what. I sort of took this up as a challenge after reading an
article the other day comparing perl vs elisp for text processing, in
which elisp was given the nod as the superior language. I could write
this text processor pretty easily in Python. But it would not be nearly
as compact as what I have so far. True, it would work.
Here is a sample of the .ini file text:
;; [customtableprofile0]
;; column0label = %%genLab_SessionViews%%
;; column0measureid = Visits
;; description = %%CT_SD_Activity_by_DMA%%
;; dimension0guid = iQTtYWnnPQ5
;; dimension0label = %%CD_On_Site_Search_Phrase%%
;; profilename = On-Site Search Phrases Performance
;; profileid = Q5DJTuwS5e6
(Not preceded by comment markers in the original.)
Any help in correcting my errors would be greatly appreciated.
Thanks.
mp
--
Michael Powe michael@trollope.org Naugatuck CT USA
"Mutable stateful objects is the new spaghetti code." - Rich Hickey
- Writing to buffer/file,
Michael Powe <=