# How to refer to the absolute row number in a \DTLforeach loop

Consider the following MWE :

\documentclass[12pt]{article}
\usepackage{datatool}
\begin{document}

\DTLnewdb{table}

\DTLnewrow{table}
\DTLnewdbentry{table}{a}{hallo}
\DTLnewdbentry{table}{b}{non}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{bonjour}
\DTLnewdbentry{table}{b}{non}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{czesc}
\DTLnewdbentry{table}{b}{oui}
\DTLnewrow{table}
\DTLnewdbentry{table}{a}{hola}
\DTLnewdbentry{table}{b}{oui}

\section{display table}

\DTLdisplaydb{table}

\section{Subtable}

\begin{tabular}{|c|c|c|}
\hline
a & b & row-number \\
\DTLforeach[\DTLiseq{\b}{oui}]{table}{\a=a,\b=b}{%
\a    &    \b & \arabic{DTLrowi}  \\
}%
\end{tabular}

\end{document}


The \DTLforeach loop runs on all rows for with the test [\DTLiseq{\b}{oui}] is true, i.e. all rows with a=oui or rows 3 and 4. The command \arabic{DTLrowi} delivers the row number of the current row in the loop, but it refers to the "subtable" generated by the restriction [\DTLiseq{\b}{oui}]. In this subtable, they are rows 1 and 2.

My question: I want to refer to the original row numbers (3 and 4 in this case). Is it possible ?

-

The <condition> associated with \DTLforeach[<condition>]{..}{..}{<body>} should then be used within <body>. Furthermore, the result of \DTLiseq{\b}{oui} returns true or false and should then be used in an \ifthenelse{<condition>}{<true>}{<false>} boolean clause:

\DTLforeach
%[\DTLiseq{\b}{oui}]
{table}
{\a=a,\b=b}{%
\ifthenelse{\DTLiseq{\b}{oui}}
{\a    &    \b & \arabic{DTLrowi}  \\}
{}%
}%


This definition in your MWE produces:

-
simple and efficient :) I'm nevertheless puzzled, that the original rownumber is not anymore available in the DTLloop with the [test] – nicolas roy Jan 20 '12 at 12:03
@nicolasroy: It is, but it is only stepped (incremented) when the <condition> is true. That's why migration to the body means it the default condition always holds (which is true), stepping the counter at every row, resulting in the correct output. – Werner Jan 20 '12 at 21:41