(dofile) - (dolist)-like file iterator

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

(dofile) - (dolist)-like file iterator

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

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

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post 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

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

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

Locked