This question led to a new feature in a package:
datatool
\DTLgdeletedb
is available in (v2.13) and later.
I am trying to use a \foreach
loop to iterate over a few items. For each iteration of the steps of \ExtractData
are:
- Load the data into
mydb
. - Extract the appropriate data.
- Delete
mydb
.
This works just fine outside of a \foreach
. But if used within a \foreach
, it appears that the database is not deleted, and any subsequent attempt to create a new database with the same name fails with:
Package
datatool
Error: Database `myDB' already exists.
Notes:
- The MWE example below, shows that that database access is working fine outside of a
\foreach
, and within the very first invocation within a\foreach
. - The failure occurs only if an attempt is made to use
\ExtractData
after a use within the\foreach
. So uncommenting any of the commented blocks will show the failure. - Before someone asks why I close and re-open the same db, in my actual usage, the data file opened is different for each member of the
\foreach
. Just the name of the data base is the same. I guess I could create a new name each time, but I'd really like to know what the actual problem is.
Reference:
- This question about Datatool : can't use \DTLreplaceentryforrow in nested loop “foreach” seems somewhat related but since I am not using a
\DTLforeach
to iterate thru the database, I don't think it helps me.
Code:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
Smith, John, 1977-05-04
Doe, Jane, 1980-05-04
\end{filecontents*}
\usepackage{xstring}
\usepackage{pgffor}
\usepackage{datatool}
\newcommand{\ExtractData}[3]{% #1=file name #2=row, #3=col
\DTLloaddb[noheader,keys={LastName, FirstName, DOB}]{myDB}{#1}%
\IfEq{\DTLrowcount{myDB}}{0}{%
\def\Value{NO VALUE}
}{%
\DTLgetvalue{\Value}{myDB}{#2}{#3}%
}%
\DTLdeletedb{myDB}%
\Value%
}
\begin{document}
\section{Works}
\ExtractData{foo.dat}{1}{2}
\ExtractData{foo.dat}{1}{1}
\section{Works in foreach (one element)}
\foreach \x in {2}{
\ExtractData{foo.dat}{1}{\x}
}
\section{Does NOT work subsequent to foreach (one element)}
%\ExtractData{foo.dat}{1}{1}
%\foreach \x in {1}{
% \ExtractData{foo.dat}{1}{\x}
%}
\section{Does NOT works in foreach (more than one element)}
%\foreach \x in {2,1}{
% \ExtractData{foo.dat}{1}{\x}\par\noindent
%}
\end{document}
\foreach
works in a group, so the\DTLdeletedb
inside the\foreach
deletes the database only locally.\DTLloaddb
and\DTLdeletedb
were within the same scope I though they would either both load/delete locally or both load/delete globally so did not think that that was the problem.\foreach
is run in a distinct group.