# How to count rows of a csv?

I am using \csvreader to read a csv and I am trying to do some basic manipulation like counting the number of values and print that value in the document. For instance, how do I calculate the number of males from the code below, and print There are a total of \malesnum males?

\documentclass{standalone}

\usepackage{csvsimple}

Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
\begin{tabular}{l|c}%
\bfseries Person & \bfseries Matr.~No.% specify table head
{\\\hline\givenname\ \name & \matriculation}% specify your coloumns here
\end{tabular}
\end{document}


Here is an example with the datatool package.

\documentclass{article}

\usepackage{datatool}

Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
{%
\DTLiffirstrow{}{\\}
\ln & \mat & \gn &  \gr %
} \\ \hline
\end{tabular}

\DTLsavelastrowcount{\nmales}

\medskip

This database has a total of \nmales{} male students.

\bigskip

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
{%
\DTLiffirstrow{}{\\}
\ln & \mat & \gn &  \gr %
} \\ \hline
\end{tabular}

\DTLsavelastrowcount{\pass}

\medskip

This database has a total of \pass{} students who had over \thePassGrade.

\end{document}


This solution uses pgfplotstable. The hard part was merging two columns into one (fullname).

\documentclass{article}

\usepackage{pgfplotstable}

Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}

\pgfplotstablegetrowsof{\mytable}%
number rows = \pgfplotsretval{} (header row doesn't count)

\pgfplotstablegetcolsof{\mytable}%
number columns = \pgfplotsretval

\pgfplotstablecreatecol[create col/assign/.code={%
\edef\entry{\thisrow{givenname}~\thisrow{name}}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{fullname}\mytable

\pgfplotstabletypeset[string type,columns={fullname,matriculation},
columns/fullname/.style={column name=\textbf{Person},column type={l|}},
columns/matriculation/.style={column name=\textbf{Matr.~No.},column type={c}}
]{\mytable}

\end{document}


For instance

\begin{filecontents*}{\jobname.csv}
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}

\newcounter{dambocnt}

\begin{document}

\setcounter{dambocnt}{0}
\newcommand{\ismale}{%
\ifnum\pdfstrcmp{\gender}{m}=0
\stepcounter{dambocnt}%
\fi
}

\begin{tabular}{l|c}%
\bfseries Person & \bfseries Matr.~No.% specify table head