newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Dmi on October 01, 2005, 01:38:24 PM

Title: (dofile) - (dolist)-like file iterator
Post by: Dmi on October 01, 2005, 01:38:24 PM
(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 :-)
Title:
Post by: Lutz on October 02, 2005, 06:20:00 AM
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
Title:
Post by: Dmi on October 02, 2005, 08:10:20 AM
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.
Title:
Post by: nigelbrown on October 02, 2005, 01:02:54 PM
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
Title:
Post by: nigelbrown on October 02, 2005, 04:09:31 PM
Quote from: "nigelbrown"Perhaps syntax something like awk.

Dmi,

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



Nigel
Title:
Post by: Dmi on October 02, 2005, 04:32:39 PM
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 :-)