As a follow-up to several questions, I've come up with the following MWE, with the aim of filling an array (via arrayjobx
) with values loaded from a CSV file.
The desired output is to print something like this:
---------------------------------------
| | John Doe (084754) |
| Students: | Carl Sagan (458736) |
| | Peanut Butter (432756) |
---------------------------------------
The CSV:
Campo,Valor,Aux
alumno,"John Doe","084754"
alumno,"Carl Sagan","458736"
alumno,"Peanut Butter","432576"
The LaTeX
document:
\documentclass{minimal}
\usepackage{arrayjobx,datatool,multirow,forloop}
\newcounter{numst}
% Offending line vvv
\newcommand{\addstudent}[2]{\expandafter\addstudentaux\expandafter{#1}\expandafter{#2}}
\newcommand{\addstudentaux}[2]{%
\stepcounter{numst}%
\ifthenelse{\equal{\value{numst}}{1}}{\newarray\Students\newarray\Boletas}{}
\Students(\arabic{numst})={#1}%
\Boletas(\arabic{numst})={#2}%
}
\newcommand{\IsEmptyElement}[2]{\ifthenelse{\boolean{emptydata}}{#1}{#2}}
\AtEndDocument{\ifthenelse{\value{numst} > 0}{\delarray\Students\delarray\Boletas}{}}
\begin{document}
\DTLloaddb{Datos}{datos.csv}
\DTLforeach{Datos}{\campo=Campo,\valor=Valor,\valorb=Aux}{%
\ifthenelse{\equal{\campo}{alumno}}{%
\addstudent{\valor}{\valorb}
}{}
}
\begin{tabular}{cc}
\ifthenelse{\value{numst} > 0}{%
\ifthenelse{\value{numst} = 1}{%
\textbf{Student:} & \Students(1) \\
\textbf{ID:} & \Boletas(1) \\
}{%
\multirow{\value{numst}}{*}{\textbf{Students:}}%
\newcounter{st}%
\forloop{st}{1}{\not{\value{st} > \value{numst}}}{%
\checkBoletas(\arabic{st})%
& \Students(\arabic{st}) \IsEmptyElement{}{(\Boletas(\arabic{st}))} \\%
}%
}
}{}
\end{tabular}
\end{document}
Output:
How can I retrieve these values correctly? \expandafter
only helps on retrieving, at most, one value successfully. Thanks in advance!
EDIT: I'm doing it this way as there'll be other data appended at the end of the table.