(dofile) - (dolist)-like file iterator

Started by Dmi, October 01, 2005, 01:38:24 PM

Previous topic - Next topic

Dmi

(dofile (l "/etc/passwd") (println ">" l))
I wrote macro that works such way.

Freely available from http://en.feautec.pp.ru/SiteNews/dofilelsp">//http://en.feautec.pp.ru/SiteNews/dofilelsp



Comments are welcome.



Thanx to Lutz for notes about programming :-)
WBR, Dmi

Lutz

#1
Very nice! Another possibility would be to use 'parse' to break up the file at the delimiter and then process the records, i.e:



(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

Dmi

#2
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.
WBR, Dmi

nigelbrown

#3
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

nigelbrown

#4
Quote from: "nigelbrown"Perhaps syntax something like awk.

Dmi,

I see you already have awklsp at http://en.feautec.pp.ru/SiteNews/awklsp">http://en.feautec.pp.ru/SiteNews/awklsp



Nigel

Dmi

#5
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 :-)
WBR, Dmi