[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: What I'm trying to do (reading a file

William James

6/8/2015 5:37:00 PM

cbbrowne wrote:

> > + I've got a file ("input.txt")
> > -------//-----------
> > 1.John;Engineer
> > 2.Anthony;Chief
> > 3.Susanne;Secretary
> > 4.Richard;Engineer
> > -------//---------
> > + I want to read this file ("input.txt"), and I want to select only
> > the characters between "." and ";", creating a list.
> >
> > I mean, from "input.txt", I want to obtain:
> > "John" "Anthony" "Susanne" "Richard"
> >
> > But I'm not able to carry it out. I would be extremely obliged if
> > anybody could provide me any help.
>
> Is this homework?
>
> Where I'd start would be something like:
>
> (format t "~A~% " (let ((r nil))
> (with-open-file
> (s "home:input.txt" :direction :input)
> (setf r
> (loop
> for line = (read-line s nil nil)
> while line
> collect (subseq line (search "." line) (search ";" line)))))
> r))
>
> It's got errors, and there are some bits that are definitely a bit
> odd. I'd not claim it to be an ideal implementation by _any_ means.
>
> But it produces meaningful output that's not _spectacularly_ different
> from what you were looking for, and hopefully provides at least food
> for thought.

Gauche Scheme:

(with-input-from-file "junk"
(lambda ()
(generator-map
(compose (cut <> 1) #/[.](.*?);/)
read-line)))

===>
("John" "Anthony" "Susanne" "Richard")


Another way:

(with-input-from-file "junk"
(lambda ()
(generator-map
(compose cadr (cut string-split <> #[.;]))
read-line)))


Another way:

(with-input-from-file "junk"
(lambda ()
(generator-map
(compose (cut string-scan <> #\; 'before)
(cut string-scan <> #\. 'after))
read-line)))

--
Swedes are now in many ways treated as second-class citizens in their own
country. The very word "Swede" has been given a negative connotation. This kind
of project has now been introduced all over Western Europe.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
1 Answer

William James

1/16/2016 9:02:00 AM

0

WJ wrote:

> cbbrowne wrote:
>
> > > + I've got a file ("input.txt")
> > > -------//-----------
> > > 1.John;Engineer
> > > 2.Anthony;Chief
> > > 3.Susanne;Secretary
> > > 4.Richard;Engineer
> > > -------//---------
> > > + I want to read this file ("input.txt"), and I want to select only
> > > the characters between "." and ";", creating a list.
> > >
> > > I mean, from "input.txt", I want to obtain:
> > > "John" "Anthony" "Susanne" "Richard"
> > >
> > > But I'm not able to carry it out. I would be extremely obliged if
> > > anybody could provide me any help.
> >
> > Is this homework?
> >
> > Where I'd start would be something like:
> >
> > (format t "~A~% " (let ((r nil))
> > (with-open-file
> > (s "home:input.txt" :direction :input)
> > (setf r
> > (loop
> > for line = (read-line s nil nil)
> > while line
> > collect (subseq line (search "." line) (search ";" line)))))
> > r))
> >
> > It's got errors, and there are some bits that are definitely a bit
> > odd. I'd not claim it to be an ideal implementation by any means.
> >
> > But it produces meaningful output that's not spectacularly different
> > from what you were looking for, and hopefully provides at least food
> > for thought.

MatzLisp (Ruby):

IO.readlines("input.dat").map{|line| line[/[.](.*);/, 1]}
===>
["John", "Anthony", "Susanne", "Richard"]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...