help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing records in lisp?


From: Oliver Scholz
Subject: Re: Parsing records in lisp?
Date: Tue, 13 May 2003 12:48:34 +0200
User-agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.3.50 (windows-nt)

address@hidden (Henrik Jönsson) writes:

> Hi!
>
> I have a file to record what I have I worked on every day. I use this
> list when I do my time reporting each week.
>
> The file looks like this:
>
> Week: 19
> #Date           Hours   Project Comment
> 2003-05-08    2       FVCI    Meeting with Prover about the quotation
> 2003-05-08    2       FVCI    Reviewing Test Spec and Req Spec
[...]

> However, I would like to create this report in lisp. I have tried
> some, but lisp is not yet my favourite language. Can anyone help me
> with this? Give me some snippets to be able to continue.

I think the most straightforward way for you is to read the stuff
into lists using regular expressions, `re-search-forward',
`looking-at' and `match-string':

[Everything below is completely untested, of course.]

(defconst my-regexp
  (concat 
   "\\([[:digit:]]\\{4\\}-[01][[:digit:]]-[0-3][[:digit:]]\\)"
   "\\s-+\\([[:digit:]]\\)"
   "\\s-+\\([[:alnum:]]+\\)"
   "\\s-+\\(.*\\)"))

(defun my-read-stuff ()

 [...]

(let ((file-list nil))
  (while (re-search-forward "^Week: \\([[:digit:]]+\\)" nil t)
    (let ((week (match-string 1))
          (week-list nil))
      (while (not (looking-at my-regexp))
        (forward-line 1))
      (while (looking-at my-regexp)
        (push (list (match-string 1)  ; date
                    (match-string 2)  ; hours
                    (match-string 3)  ; project
                    (match-string 4)) ; comment
              week-list)
        (forward-line 1))
      (push (cons week week-list) file-list)))

 [...]

 file-list)
  

And write it out again using `insert' (and maybe `format'):

(defconst my-separator (make-string 15 ?#))

(defun my-write-stuff ()
  (interactive)

  [...]

  (with-current-buffer [...]
    (let ((contents (my-read-stuff)))

      [...]

      (dolist (week contents)
        (insert my-separator "\n")
        (insert (format "Week: %s" (car week)))
        (let ((time-accounts (my-calculate-projects-time
                              (cdr week))))
          ;; `my-calculate-projects-time' (or whatever name you
          ;; choose) should return a list of projects of the form:
          ;; (ACCOUNT ACCOUNT*)
          ;; Where account is a cons cell: (PROJECT . TIME).
          ;; Use `cons' to build cons cells. Use `string-to-number' to
          ;; get the numbers from the strings.
          (dolist (entry time-accounts)
            (insert (format "Project %s: %s"
                            (car entry) (cdr entry))))))

      [...])

HTH

    Oliver
-- 
24 Floréal an 211 de la Révolution
Liberté, Egalité, Fraternité!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]