[Tex/LaTex] How to read a file line by line and store each line into an array

data structures

As said in the title, I am looking for a way to read a file line by line (done!) and store each line, each string, in an array.

I have searched the web and stackexchange for a while but I cannot find a suitable solution—in fact, I still do not know, whether arrays as data type do exist or not.

I have no MWE, yet, because of the lack of information.

\newread\file
    \openin\file=myfilename.txt
    \loop\unless\ifeof\file
        \read\file to\fileline % Reads a line of the file into \fileline
            \fileline
    \repeat
\closein\file

This is my construct of reading a file line by line, which seems to work properly. \fileline is the string I want to store in an array or somewhat equal to an array. In Java I would use an ArrayList;

ArrayList<String> list = new ArrayList<String>();

would be the object and

list.add(readline);

would be the statement to store the parameter readline into list.


Ok, well. I fear those solutions are much too complicated for me to understand, in fact, I have no idea what is going on in all of your answers. My problem, the solution, respectively, seems to be way too difficult.

I would like to read a file which contains exactly one character per line and then store it in an array or whatever type of list, so that I will be able to read the array with myarray[2] or something to get the second (or third, depending on whether to start at 1 or 0) value from the list to do things with it.

I will examine the datatool package further—at first sight it seems vaguely to do what I want, but I do not know how, yet.

Thanks for all of your replies and I am sorry to have wasted your time!

Regards
hringriin

EDIT:

Ok, well. I fear those solutions are much too complicated for me to understand, in fact, I have no idea what is going on in all of your answers. My problem, the solution, respectively, seems to be way too difficult.

I would like to read a file whichs contains exactly one character per line and then store it in an array or whatever type of list, so that I will be able to read the array with myarray[2] or something to get the second (or third, depending on whether to start at 1 or 0) value from the list to do things with it. So my problem itself is the way of storing the data or the chars from the file in LaTeX. The problem is that I cannot read a single line from a file.

I will examine the datatool package further—at first sight it seems vaguely to do what I want, but I do not know how, yet.

Best Answer

It's not really difficult with expl3. I define a command \ReadFile that takes two arguments

\ReadFile{\myarray}{somefile.dat}

The first argument is a control sequence name, the second is the file to read from. With this, the file will be read in and the command \myarray defined so that

\myarray{2}

yields the second item; the special call \myarray{*} will return the number of items. You can also call \myarray{-1} to access the last item.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\ior_new:N \g_hringriin_file_stream

\NewDocumentCommand{\ReadFile}{mm}
 {
  \hringriin_read_file:nn { #1 } { #2 }
  \cs_new:Npn #1 ##1
   {
    \str_if_eq:nnTF { ##1 } { * }
      { \seq_count:c { g_hringriin_file_ \cs_to_str:N #1 _seq } }
      { \seq_item:cn { g_hringriin_file_ \cs_to_str:N #1 _seq } { ##1 } }
   }
 }

\cs_new_protected:Nn \hringriin_read_file:nn
 {
  \ior_open:Nn \g_hringriin_file_stream { #2 }
  \seq_gclear_new:c { g_hringriin_file_ \cs_to_str:N #1 _seq }
  \ior_map_inline:Nn \g_hringriin_file_stream
   {
    \seq_gput_right:cx 
     { g_hringriin_file_ \cs_to_str:N #1 _seq }
     { \tl_trim_spaces:n { ##1 } }
   }
  \ior_close:N \g_hringriin_file_stream
 }

\ExplSyntaxOff

\begin{document}

\ReadFile{\myarray}{somearray.dat}

\myarray{*}

\myarray{1}

\myarray{2}

\myarray{3}

\myarray{-1}

\myarray{-2}

\myarray{-3}

\end{document}

If the file somearray.dat is

And now for something completely different 
1 2 3 4 5 6 7 8
a bc def ghij

(I used the same as Christian), the result will be

enter image description here

Related Question