Is there an easy way to read arbitrary lines from a file? For example "read line number 12" or "read backwards from last line to first line" or "read lines 1,3,5...".
|
|
No, you can't read arbitrary lines, but you can't do that in most other programming languages either. You have to read line after line from the beginning.
Apart from the
|
|||
|
|
|
What you are asking about here is "random access" to the lines of text file, which is not supported by any filesystem that I know of. Most programming languages have a Unfortunately, most text files have a varying number of bytes per line so it is impossible to predict the offset at which a given line begins without reading the contents of the file. To get around this problem, you need to "index" the file in some way such that lines can be located in an arbitrary order. Two methods that I am aware of are:
Suppose we have the following input file:
With TeX you have two problems:
Solution: Use LuaTeXThis is the easiest way to go since Lua tables make great array objects:
The
Lua |
||||
|
|
|
TeX is able to read lines from a file as long as they have balanced braces. This is accomplished through \openin and \read.
Now \command will expand to the contents of the first line in file.dat and a subsequent It must be mentioned the possibility of calling, via "shell-escape", external utilities that may be faster and more flexible. |
|||
|
|
