I want to ensure in my LaTeX code, that my .csv input data which will be printed within an \pgfplotstable environment are valid or at least will not end up in a compiler error. So the LaTeX code needs to be so "secure", that anyone can edit the input data and when there is a fault, I will report the concrete error reason to the end user via a pdf comment. Of course this could also be done with other more high level tools, but this is a good project for me to learn a lot of how LaTeX works :)
The error handling is almost done, but I need to check for a dedicated pattern in the first (2) rows.
To be maximum variable and extendable, I created a function, where I just need to put in a defined pattern as an array and all the rest should work automatically.
For this, I wrote a more or less complicated routine to check the content about the first rows. This seems to work apart from this line: \noexpand\ifthenelse{\noexpand\equal{\noexpand\pgfplotsretval}{\noexpand\elementY}} There seems to be an expansion issue, but when you look at the output it should be correct.
MWE
\documentclass[10pt,oneside,a4paper]{article}
\usepackage{ifthen}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\setlength\parindent{0pt}
\def\truncdiv#1#2{((#1-(#2-1)/2)/#2)}%
\def\moduloop#1#2{(#1-\truncdiv{#1}{#2}*#2)}%
\def\modulo#1#2{\number\numexpr\moduloop{#1}{#2}\relax}%
\def\firstRowArray{{A},{B},{C}}
\def\secondRowArray{{D},{E},{F}}
\def\combinedArray{{\noexpand\firstRowArray},{\noexpand\secondRowArray}}
\makeatletter
\newcommand{\countlist}[1]{%
\count@=\z@
\@for\next:=#1\do{\advance\count@\@ne}%
\the\count@
}
\makeatother
\begin{filecontents}{data.csv}
A;B;C
D;E;G
?!;?!;?!
\end{filecontents}
\pgfplotstableread[col sep=semicolon]{data.csv}{\csvdata}
% Check the content of the first rows
\newcommand{\checkFirstRows}[1]{
\newcounter{columnCounter}
\setcounter{columnCounter}{0}
\newcounter{rowCounter}
\setcounter{rowCounter}{-1}
\def\maxColumnCount{\countlist{\firstRowArray}}
\foreach \elementX in #1 {
RowCounter:$\the\value{rowCounter}$\newline\newline
\begingroup
\edef\temp{\endgroup
\noexpand\foreach \noexpand\elementY in \elementX {
ColumnCounter:\noexpand\the\value{columnCounter}\newline
ColumnCnt:\noexpand\maxColumnCount\newline
\noexpand\ifthenelse{\value{columnCounter}<3}{
\noexpand\pgfplotstablegetcolumnnamebyindex{\value{columnCounter}}\noexpand\of{\noexpand\csvdata}\noexpand\to\noexpand\pgfplotsretval
RET-1:\noexpand\pgfplotsretval\newline
ELE-Y1:\noexpand\elementY\newline
\noexpand\ifthenelse{\noexpand\equal{\noexpand\pgfplotsretval}{\noexpand\elementY}}{
YES\newline
}{
NO\newline
}
}{
\noexpand\gdef\noexpand\modValue{\noexpand\modulo{\noexpand\the\value{columnCounter}}{3}}
MOD:\noexpand\modValue\newline
\noexpand\pgfplotstablegetelem{\value{rowCounter}}{[index]\noexpand\modValue}\noexpand\of{\noexpand\csvdata}
RET-2:\noexpand\pgfplotsretval\newline
ELE-Y2:\noexpand\elementY\newline
\noexpand\ifthenelse{\noexpand\equal{\noexpand\pgfplotsretval}{\noexpand\elementY}}{
YES\newline
}{
NO\newline
}
}
\noexpand\stepcounter{columnCounter}
}
}\temp
\stepcounter{rowCounter}
}
}
\pgfplotstableset{verb string type}
\begin{document}
\pgfplotstabletypeset{\csvdata}\\\\\\
\checkFirstRows{\combinedArray}
\end{document}
OUTPUT
EXPECTED OUTPUT (without counter values)
RET-1:A
ELE-Y1:A
YES:1
RET-1:B
ELE-Y1:B
YES:1
RET-1:C
ELE-Y1:C
YES:1
=> Only YES until here would mean the header is correct!
RET-2:D
ELE-Y2:D
YES:2
RET-2:E
ELE-Y2:E
YES:2
RET-2:G
ELE-Y2:F
NO:2 => Failure! I got a "G" but an "F" is expected!
So basically, I would like to know the following two things:
- Why
\noexpand\ifthenelse{\noexpand\equal{\noexpand\pgfplotsretval}{\noexpand\elementY}}never returns "YES" - It would be great to get a hint how to replace the magic number
3with the dynamically generated number:\def\maxColumnCount{\countlist{\firstRowArray}}

"". Secure in this case just means, that my dad who is also working with this input file will not destroy anything^^