Hi,
I'm new to Lisp. I accidently volunteered to take over ownership of a Lisp
file and now I have to make some changes to it.
What i want to do is start at the beginning of the file( which I can do)
and search for the word "entity". Once I find this, I want to assign the
next word to a variable.
My proplem is that I can't figure out what stream to look at as it moves
from word to word and, thus, I can't do a correct comparison.
Help.
--
=================================================================
Bob Pack ·····@gl.pitt.edu
University of Pittsburgh
PittCAD Design Group
EEFL: Pittsburgh LambdaChi's ( LCA -- Col Div )
"It's a dog-eat-dog world and I'm wearing milk bone underwear."
In article <····@blue.cis.pitt.edu.UUCP> ·····@cislabs.pitt.edu (Robert L Pack) writes:
>What i want to do is start at the beginning of the file( which I can do)
>and search for the word "entity". Once I find this, I want to assign the
>next word to a variable.
>
>My proplem is that I can't figure out what stream to look at as it moves
>from word to word and, thus, I can't do a correct comparison.
I don't see the need to read the stream word by word. If you use READ-LINE
to read each line of the file into a string, you can then do something
like:
(defun word-after-entity (string)
"If STRING contains "entity <word>" returns <word>, otherwise returns NIL."
(let ((position (search "entity " string :test #'char-equal)))
(when position
(word-at-position string (+ position (length "entity "))))))
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar
Date: Tue, 29 Sep 1992 20:11 EDT
From: Robert L Pack <·····@cislabs.pitt.edu>
... What i want to do is start at the beginning of the file( which I can do)
and search for the word "entity". Once I find this, I want to assign the
next word to a variable. ...
Use WITH-OPEN-FILE to open the stream. It will let you specify a variable
to which the stream is bound. The I/O routines all take a stream as argument. e.g.,
(defun find-word-association-in-file (word file)
(with-open-file (stream file :direction :input)
(loop
(let ((line (read-line stream nil nil)))
(declare (type string line))
(unless line (return nil))
(let ((pos1 (position #\Space line :test #'char-equal)))
(when (and pos1 (string-equal word line :end2 pos1))
(let ((pos2 (position #\Space line
:test (complement #'char-equal)
:start pos1)))
(when pos2
(return (subseq line pos2
(position #\Space line
:test #'char-equal
:start pos2)))))))))))
Given a data file "delete-me.text" containing:
FOO OOF
BAR RAB XYZZY
BAZ ZAB PLOVER PLUGH
NUL NIL
I find that:
(find-word-association-in-file "FOO" "delete-me.text") => "OOF"
(find-word-association-in-file "BAZ" "delete-me.text") => "ZAB"
(find-word-association-in-file "NUL" "delete-me.text") => "NIL"
(find-word-association-in-file "GEE" "delete-me.text") => NIL
So you'd do
(setq my-var (find-word-association-in-file ...))