[comp.sys.mac.hypercard] Need Help with Text File Searching

consp22@bingsune.cc.binghamton.edu (Darren Handler) (02/08/90)

I have a question for some of you more experienced HyperCard programmers.
I am going to be working on a stack to do some definition lookups on selected
words.  Does anybody have good ways to search through an external textfile
for a word.  The user will select a word from an onscreen field.  The program
will then have to access a dictionary on disk containing definitions that

Word
Definition
Definition
Definition

I want ot be able to display the word in one field on the screen and then the 
three lines of definition in another.  Any help would be appreciated.

Please E-mail if possible.

Thanks alot.

-------------------------------------------------------------------------------
|  Consp22@Bingsuns.pod.binghamton.edu  |  SUNY-B Computer Consultants -      |
|  Consp22@Bingvaxu.cc.binghamton.edu   |  Trying to keep the world safe from |
|---------------------------------------|  the SUNY-B Computer users.         |
|  System Consultant - World Computers  |-------------------------------------|
|  Computer Cons. - SUNY Binghamton     |     Darren `Mac Hack' Handler       |
|-----------------------------------------------------------------------------|
I don't know if I am going to heaven or hell, I just hope God grades on a curve

sirkm@ssyx.ucsc.edu (Greg Anderson) (02/10/90)

I tried to send this via mail, but it bounced.  The following describes a
"grep" XFCN I wrote & its pertinence to searching for definitions in a
file.

-----------------

I have an XFCN that _might_ help you.  Your file format currently is:

Word
Definition
Definition
Definition

If you could change this to:

Word
 Definition
 Definition
 Definition

Or:

Word\
Definition\
Definition\
Definition

Or perhaps even:

Word*Definition*Definition*Definition

Then you could use my XFCN.  If changing your data format is inconvenient,
then perhaps you could modify my source code so that it searches four-line
blocks instead of single-line blocks.  It has modes that allows it to
consider multiple lines as one single line if subsequent lines are indented
(as in the first example) or if the end of continued lines have trailing
backslashes (second example).

My XFCN is called "grep", as it works like the unix grep command.
You would do the following:

	put readfile(thefilename) into tmpvar
	put grep("-im","^Word",tmpvar) into tmpresult
	if tmpresult is not empty then
		put line 1 of tmpresult <wherever you want it>
		delete line 1 of tmpresult -- delete 'Word'
		put tmpresult <wherever you want the definition>
	end if

In the "grep" line, the "-im" selects the flags 'i' (case insensitive
search) and 'm' (multi-line search, as in the first example).  The
"^" before "Word" indicates that grep should only look for lines that
BEGIN with "word" as opposed to all lines that contain "word".

Grep will return all lines that contain a match.  If your definition lines
are all indented, they will be considered to be part of the same line
as 'word', and will therefore be returned.

The advantage of this approach is that your definitions are no longer
constrained to three lines.

  ___\    /___               Greg Anderson              ___\    /___ 
  \   \  /   /         Social Sciences Computing        \   \  /   /
   \  /\/\  /    University of California, Santa Cruz    \  /\/\  /
    \/    \/              sirkm@ssyx.ucsc.edu             \/    \/