[comp.text.tex] TeX guru help needed

pfeiffer@cix.cict.fr (Daniel Pfeiffer) (06/25/91)

I want to read and parse every line in a file that contains no
empty lines:

	\openin\@std \bookname.sind
	\loop
		\read\@std to\@theline
		\ifx\@theline\par\encorefalse\fi
	\ifencore
		\expandafter\@parse\@theline
		\ifx\@theword\@theoldword
			\ifx\@thepage\@theoldpage\else, \@thepage\fi
		\else
			\relax\endgraf\@theword\@dots\@thepage
			\let\@theoldword=\@theword
		\fi
		\let\@theoldpage=\@thepage
	\repeat

I read from a file whose lines are all uniform, and recognized by \@parse.
When I get to the end of the file the value of \@theline is \par.  So I
figured I'd test for the end with \ifx\@theline\par.  Problem is that
\loop tries to read everything upto \repeat but falls flat on it's face
when it sees the \par.  How do I make \par symbolic rather than a paragraph
separator.  I've tried \let\@par=\par but then the test \ifx\@theline\@par
fails.  Putting an \outer before \loop doesn't change anything.

How do I correct this???  Alternate solutions to parsing every line
in a file are also welcome!

 +---------------+
 |     * * *     |
 |   *       *   |	Beware - polyglot esperantist
 |  *         *  |
 |   *       *   |
 |     * * *     |
 +---------------+
 |
 |  Daniel Pfeiffer				<pfeiffer@cix.cict.fr>
 |  Tolosa (Toulouse), Midi-Pyrenees, Europe	<pfeiffer@irit.fr>
 |						<pfeiffer@frcict81.bitnet>
 |

texinfo1@rusmv1.rus.uni-stuttgart.de (Rainer Schoepf) (06/26/91)

In article <PFEIFFER.91Jun25101651@cix.cict.fr> pfeiffer@cix.cict.fr (Daniel Pfeiffer) writes:
>I want to read and parse every line in a file that contains no
>empty lines:
>
>	\openin\@std \bookname.sind
>	\loop
>		\read\@std to\@theline
>		\ifx\@theline\par\encorefalse\fi
>	\ifencore
>		\expandafter\@parse\@theline
>		\ifx\@theword\@theoldword
>			\ifx\@thepage\@theoldpage\else, \@thepage\fi
>		\else
>			\relax\endgraf\@theword\@dots\@thepage
>			\let\@theoldword=\@theword
>		\fi
>		\let\@theoldpage=\@thepage
>	\repeat
>
>I read from a file whose lines are all uniform, and recognized by \@parse.
>When I get to the end of the file the value of \@theline is \par.  So I
>figured I'd test for the end with \ifx\@theline\par.  Problem is that
>\loop tries to read everything upto \repeat but falls flat on it's face
>when it sees the \par.  How do I make \par symbolic rather than a paragraph
>separator.  I've tried \let\@par=\par but then the test \ifx\@theline\@par
>fails.  Putting an \outer before \loop doesn't change anything.
>
>How do I correct this???  Alternate solutions to parsing every line
>in a file are also welcome!
>
> +---------------+
> |     * * *     |
> |   *       *   |	Beware - polyglot esperantist
> |  *         *  |
> |   *       *   |
> |     * * *     |
> +---------------+
> |
> |  Daniel Pfeiffer				<pfeiffer@cix.cict.fr>
> |  Tolosa (Toulouse), Midi-Pyrenees, Europe	<pfeiffer@irit.fr>
> |						<pfeiffer@frcict81.bitnet>
> |


How about testing for end-of-file with the \ifeof primitive?

Rainer Sch"opf

cet1@cl.cam.ac.uk (C.E. Thompson) (06/26/91)

In article <PFEIFFER.91Jun25101651@cix.cict.fr> pfeiffer@cix.cict.fr (Daniel Pfeiffer) writes:
>I want to read and parse every line in a file that contains no
>empty lines:
>
>	\openin\@std \bookname.sind
>	\loop
>		\read\@std to\@theline
>		\ifx\@theline\par\encorefalse\fi
>	\ifencore
{rest of code omitted}
>	\repeat
>
>I read from a file whose lines are all uniform, and recognized by \@parse.
>When I get to the end of the file the value of \@theline is \par.  So I
>figured I'd test for the end with \ifx\@theline\par.  Problem is that
>\loop tries to read everything upto \repeat but falls flat on it's face
>when it sees the \par.  ...

As Rainer Sch"opf has pointed out, using \ifeof\@std is much the easiest
and clearest way to make the test. However, it may be worth pointing out
that the \ifx test wouldn't work even if you had managed to get the \par
into the \loop. \@theline is a macro containing only \par at this time, not
\par itself. You could write

    \def\@lastline{\par}
    \loop
       ...
       \ifx\@theline\@lastline ...

and that solves both problems.

Chris Thompson
JANET:    cet1@uk.ac.cam.phx
Internet: cet1%phx.cam.ac.uk@nsfnet-relay.ac.uk

marcel@cs.caltech.edu (Marcel van der Goot) (06/27/91)

Daniel Pfeiffer (pfeiffer@cix.cict.fr) wrote:
> I want to read and parse every line in a file that contains no
> empty lines:
> [...]
> I read from a file whose lines are all uniform, and recognized by \@parse.
> When I get to the end of the file the value of \@theline is \par.  So I
> figured I'd test for the end with \ifx\@theline\par.  Problem is that
> \loop tries to read everything upto \repeat but falls flat on it's face
> when it sees the \par.

The reason for this is that \loop is not defined as a \long macro. You
could redefine \loop so that it is. However, as Rainer Sch\"opf pointed
out, the preferred way of testing for the end of file is with \ifeof.

>   Alternate solutions to parsing every line in a file are also welcome!

I think you might find Midnight/dolines.tex [.doc] useful. Also, since I'm
advertising anyway, I think that Midnight/loop.tex is more convenient to
use than plain TeX's \loop macro. (It doesn't have \long definitions either;
I'll correct that in future releases.)

dolines.tex, dolines.doc, loop.tex, and loop.doc are available from
ymir.claremont.edu [134.173.4.23], directory [tex.inputs.midnight].
(You need loop.tex to use dolines.tex.)


                                          Marcel van der Goot
 .----------------------------------------------------------------
 | Blauw de viooltjes,                    marcel@vlsi.cs.caltech.edu
 |    Rood zijn de rozen;
 | Een rijm kan gezet
 |    Met plaksel en dozen.
 |