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

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

simple first emacs script


From: Tom
Subject: simple first emacs script
Date: Wed, 15 Dec 2010 12:55:24 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Lightning/1.0b2 Thunderbird/3.1.7

I am about 2/3 way through the introduction to emacs lisp, and thought it would help my learning if I wrote tried writing a simple script to do something useful to me with the lessons I have covered so far so far. I should point out I am surgeon not a computer scientist and know nothing more about programming than what is contained in the introduction to emacs lisp.
I end up with this:

(defun nirs-data-clean (number-of-channels)
"Cleans the output files from the INVOS monitor removing all columns apart from StO2 values, and the date and time stamps. Sto2 columns to be retained are specified by the NUMBER-OF-CHANNELS variable. It assumes that channels are always used in numerical order, i.e. channel 1 always, then 2, then 3 then 4."
(interactive "nEnter number of channels (1-4): ")
(barf-if-buffer-read-only)
(require 'csv-mode)
(push-mark (point-max))
(goto-char (point-min))
(if (or (eq number-of-channels 1)
        (eq number-of-channels 2)
        (eq number-of-channels 3)
        (eq number-of-channels 4))
    (or (when (eq number-of-channels 1)
(csv-kill-fields '(4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34) (point) (mark)))
        (when (eq number-of-channels 2)
(csv-kill-fields '(4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34) (point) (mark)))
        (when (eq number-of-channels 3)
(csv-kill-fields '(4 5 6 7 8 9 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34) (point) (mark)))
        (when (eq number-of-channels 4)
(csv-kill-fields '(4 5 6 7 8 9 11 12 13 14 15 16 18 19 20 21 22 23 25 26 27 28 29 30 31 32 33 34) (point) (mark)) (message "%s is not a valid number of channels. Please enter a number between 1 and 4." number-of-channels))))
(when (y-or-n-p "Replace 0 StO$_2$ values with na: ")
    (progn (goto-char (point-min))
           (while (re-search-forward "[^[:digit:]][0][^[:digit:]]" nil t)
             (replace-match "na ")))))

The idea is to automatically clean unwanted columns from csv (space separated) data file and ask me if I want to replace 0 values with na. I appreciate this is very messy, and I should be able to specify ranges of field rather than listing every single one (but I couldn't get that to work), but it works kind of. When I evaluate it and run it for the first time it works, but if I undo the changes to the file and run it again it often only takes out the 4th column, if I reopen the file or re-evaluate the script it usually works again but I can't seem to work the pattern of working and not working - as I can't see the pattern I can't learn where I am going wrong.

I have attached a small sample data file with 4 channels being used to illustrate the kind of file this is supposed to work on.

I assume I have made an obvious mistake but I don't have the experience to know what it is. Any general comments about simple/better ways to clean this script would be welcome, for example would I be better making the regexp replace a yes/no arg to the function so if I call this script from another script I can specify a y/n argument (I don't know how to do this anyway though).

Tom

Attachment: 101108N.R14
Description: Text document


reply via email to

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