How to loop through reading .txt files while conditioning on line no.?

In summary, the code loops through three files, english.txt, spanish.txt, and line-no.txt, and prints out each line from english.txt and spanish.txt in the format "english: spanish" until it reaches the line number specified in line-no.txt. It then repeats this process until it reaches the end of line-no.txt. It uses the \unless macro from \epsilon-\TeX to check for the end of line-no.txt.
  • #1
Eclair_de_XII
1,083
91
TL;DR Summary
I have two files with the same line count. I want to read both of them in tandem and pass each line of said files as arguments to some two-parameter macro. I also want to print something on the line numbers specified by a different file I'm reading. How would I accomplish this?
style.sty:
\newcommand{\printNum}[2]{%
#1 #2%
}

\newread\readEng
\newread\readEsp

\newread\readLineNo

\newcount\myLineCount
\myLineCount=0

\newcommand{\printNumRow}{\loop\global\advance\myLineCount by 1\read\readEng to \Eng \read\readEsp to \Esp \printNum{\Eng}{\Esp}\ifnum\myLineCount=\linenum \read\readLineNo to \linenum \par The next line number to stop at is: \linenum\par\repeat}

main.tex:
\documentclass{minimal}

\usepackage{style}

\begin{document}

\input{body}

\end{document}

body.tex:
\openin\readLineNo=line-no.txt

\openin\readEng=english.txt
\openin\readEsp=spanish.txt

\read\readLineNo to \linenum

%\loop\advance\myLineCount by 1 \ifnum\myLineCount=\linenum \read\readLineNo to \linenum \par The next line number to stop at is: \linenum\par\else \read\readEng to \Eng \read\readEsp to \Esp \printNum{\Eng}{\Esp}\repeat

\printNumRow{}
\printNumRow{}
\printNumRow{}

\closein\readEng
\closein\readEsp

\closein\readLineNo

For some reason, the loop in line 8 of body.tex only runs the one time. I'm guessing the loop terminated because the \ifnum condition was satisfied. The \printNumRow{} calls were work-arounds to the problem. Can anyone think of a better one?
 

Attachments

  • english.txt
    32 bytes · Views: 171
  • line-no.txt
    7 bytes · Views: 159
  • spanish.txt
    36 bytes · Views: 172
Physics news on Phys.org
  • #2
If anyone's interested, I fixed my code. It doesn't actually work, though, unless you typeset this using \epsilon-\TeX or \LaTeX, since I'm calling the \unless macro from \epsilon-\TeX.

style.tex:
\def\printNum{
\read\readEng to \Eng
\read\readEsp to \Esp
\Eng\ \Esp\par}

\newread\readEng
\newread\readEsp
\newread\readLineNo

\def\openAll{
\openin\readEng=english.txt
\openin\readEsp=spanish.txt
\openin\readLineNo=line-no.txt
}

\def\closeAll{
\closein\readEng
\closein\readEsp
\closein\readLineNo
}

\newcount\myLineCount

\def\printEngEspDict{
\loop
\read\readLineNo to \linenum
\unless\ifeof\readLineNo

{\loop
\ifnum\myLineCount=\linenum
    \relax\number\myLineCount
\else
    \printNum
\global\advance\myLineCount by 1
\repeat}

\repeat
}

body.tex:
\input{style}

\openAll

\printEngEspDict

\closeAll
 
Last edited:

FAQ: How to loop through reading .txt files while conditioning on line no.?

How do I loop through a .txt file in my code?

To loop through a .txt file in your code, you can use the built-in function open() to open the file and then use a for loop to iterate through each line in the file.

How can I condition my loop to only read certain lines in the .txt file?

To condition your loop on specific lines in the .txt file, you can use the if statement within your for loop to check the line number and only read the lines that meet your condition.

Can I use a while loop instead of a for loop to read through the .txt file?

Yes, you can use a while loop to read through a .txt file, but you will need to use the readline() function to read each line individually and increment a counter variable to keep track of the line number.

How do I handle errors while reading through a .txt file?

You can use a try-except block to handle any potential errors that may occur while reading through a .txt file. This will allow you to catch and handle any exceptions that may arise, such as file not found or invalid file format.

Is there a more efficient way to loop through a .txt file while conditioning on line number?

There are several ways to increase the efficiency of looping through a .txt file while conditioning on line number. One way is to use the enumerate() function to keep track of both the line number and the line itself in the for loop. Another way is to use the linecache module, which allows you to directly access specific lines in a file without iterating through the entire file.

Back
Top