# When DTLforeach is used with a condition, DTLiflastrow does not work

In the following code, my dataset is {1,2,3,4}.

With no filter, DTLiffirstrow will return true at "1" and DTLiflastrow will return true at "4". This is what I expect from these functions.

When I filter the dataset to {2,3}, DTLiffisrtrow returns true at "2". Perfect.

But DTLiflastrow is always false. I would expect it to return true for the value "3".

\documentclass{article}

\usepackage{datatool}

\DTLnewdb{table}
\DTLnewrow{table}
\DTLnewdbentry{table}{C1}{1}
\DTLnewrow{table}
\DTLnewdbentry{table}{C1}{2}
\DTLnewrow{table}
\DTLnewdbentry{table}{C1}{3}
\DTLnewrow{table}
\DTLnewdbentry{table}{C1}{4}

\begin{document}

\DTLforeach*%
[\DTLisclosedbetween{\COne}{2}{3}]%
{table}%
{\COne=C1}%
{%
\COne%
\DTLiffirstrow{FIRST}{}%
\DTLiflastrow{LAST}{}%
\\%
}%

\end{document}


How could I detect the last row of a filtered set of data?

• There's no way for datatool to know it's on the last filtered row on a single pass. It would require two passes: first to find out which is the last row of the filtered set and the second to compare the current row with that value. (datatool knows how many rows the database has but the last row may be filtered. \DTLiflastrow can only compare the current row index with the total number of rows.) – Nicola Talbot Apr 1 at 15:23

\DTLiflastrow{〈true part 〉}{〈false part 〉}