Page 1 of 1

(dofile) - (dolist)-like file iterator

Posted: Sat Oct 01, 2005 8:38 pm
by Dmi

Code: Select all

(dofile (l "/etc/passwd") (println ">" l))
I wrote macro that works such way.
Freely available from http://en.feautec.pp.ru/SiteNews/dofilelsp

Comments are welcome.

Thanx to Lutz for notes about programming :-)

Posted: Sun Oct 02, 2005 1:20 pm
by Lutz
Very nice! Another possibility would be to use 'parse' to break up the file at the delimiter and then process the records, i.e:

Code: Select all

(dolist (rec (parse (read-file "/etc/passwd") "\n")) 
    (println ">" rec))
wrap you macro logic around it to isolate the loopvariable, filename and string delimiter and perhaps it comes out much shorter.

Lutz

Posted: Sun Oct 02, 2005 3:10 pm
by Dmi
Yes, I know about (parse (read-file)). It pretty small itself to be sufficient without (dofile).

But if the file is many-megabytes sendmail log, sales report for a year or so, then reading whole file will not be memory efficient... Moreover, we usually need only few fields from each record... And usually a few records from whole file.

I trying to write unix-like filter, that will conform to lispish processing style.

Posted: Sun Oct 02, 2005 8:02 pm
by nigelbrown
Perhaps syntax something like awk.
Then rather than do-file the command line would feed multiple files/file names in. viz:

(BEGIN (setq total 0))
( (regex "lab+") (inc total))
(END (println total))

where regex with 1 parameter matches against the automatic readline.

Nigel

Posted: Sun Oct 02, 2005 11:09 pm
by nigelbrown
nigelbrown wrote:Perhaps syntax something like awk.
Dmi,
I see you already have awklsp at http://en.feautec.pp.ru/SiteNews/awklsp

Nigel

Posted: Sun Oct 02, 2005 11:32 pm
by Dmi
Yes. I like awk :-)
But my exersices show that awk.lsp gives more awkish, than lispish and while coding in newlisp I prefer to go slightly different way.

Nevertheless (rawk) in awk.sp is either good, I think :-)