[Tex/LaTex] csvsimple filtering problem

conditionalscsvcsvsimple

The following code works as I would expect.

\documentclass{scrartcl}
\usepackage{csvsimple,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\csvreader[no head,before filter= \ifthenelse{\thecsvrow<4}{\csvfilteraccept}{\csvfilterreject}]{chunk.csv}{}{%
\begin{itemize} 
\item \csvcoli , \csvcolii
\end{itemize}}

\end{document} 

When I change the filter condition from "\thecsvrow<4" to "\thecsvrow=4" or "\thecsvrow>4" the code breaks. Pointers as to how to make this work would be greatly appreciated.

Best Answer

The per-item testing should be performed when setting the item, rather than in the header, since the filter are set globally. From the csvsimple documentation:

\csvfilterreject

All following data lines will be ignored. This command overwrites all previous filter settings.

enter image description here

\documentclass{article}
\usepackage{csvsimple,filecontents}% http://ctan.org/pkg/{csvsimple,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}<4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}=4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}>4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\end{document}

Low-level conditionals are used to (not) print each item, testing the value of csvrow.


Here is an alternative option using datatool. The interface is very similar, and the transition should therefore not be a problem:

enter image description here

\documentclass{article}
\usepackage{datatool,filecontents}% http://ctan.org/pkg/{datatool,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\DTLloaddb[noheader,keys={first,last}]{chunk}{chunk.csv}
\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}<4\relax\item \First, \Last\fi}
\end{itemize}

\hrulefill

\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}=4\relax\item \First, \Last\fi}
\end{itemize}

\hrulefill

\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}>4\relax\item \First, \Last\fi}
\end{itemize}

\end{document}

Low-level conditionals are used to (not) print each item, testing the value of DTLrowi.