Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

After a pair of \DTLnewrow/\DTLnewdbentry which creates two rows with the key1 data, I want to add the key2 data to the earlier row. I thought that \dtlgetrow{myDB}{1} would make the current row the one with rowid=1, but that does not appear to be the case as the data just got added to second row.

The MWE below yields:

enter image description here

Question: What do I replace instead of \dtlgetrow{myDB}{1} so that I obtain the results where xprime and NULL are swapped. Basically, how can I select which row is the current row for subsequent operations?

Notes:

  • A simple solution would be to change the order in which I do these operations, but I can't easily do that.

Code:

\documentclass{article}
\usepackage{datatool}

\begin{document}
\DTLnewdb{myDB}

\DTLnewrow{myDB}% create first row
\DTLnewdbentry{myDB}{key1}{x}

\DTLnewrow{myDB}% create second row
\DTLnewdbentry{myDB}{key1}{y}

\dtlgetrow{myDB}{1}% ????? Thought this would make row 1 the current row

\DTLnewdbentry{myDB}{key2}{xprime}% This needs to be in the first row
\DTLdisplaydb{myDB}
\end{document}
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

\dtlgetrow gets the given row and stores it in the current token register \dtlcurrentrow. Only those commands listed in the "Operating on Current Row" section of the manual affect this register. Those commands modify the token register not the database. Once the modifications are made, you can merge the changes back into the database using \dtlrecombine.

\documentclass{article}
\usepackage{datatool}

\begin{document}
\DTLnewdb{myDB}

\DTLnewrow{myDB}% create first row
\DTLnewdbentry{myDB}{key1}{x}

\DTLnewrow{myDB}% create second row
\DTLnewdbentry{myDB}{key1}{y}

\dtlgetrow{myDB}{1}% store row 1 in \dtlcurrentrow register
\dtlappendentrytocurrentrow{key2}{xprime}% make local modification to current row
\dtlrecombine% merge modification into database.

\DTLdisplaydb{myDB}

\end{document}

This is the most efficient way to edit a particular row in the database.

share|improve this answer
add comment

enter image description here

I can't see why you can't just put \value{DTLrowi}=1 in the optional argument to filter the rows, but that generates an error, but this works:

\documentclass{article}
\usepackage{datatool}

\begin{document}
\DTLnewdb{myDB}

\DTLnewrow{myDB}% create first row
\DTLnewdbentry{myDB}{key1}{x}

\DTLnewrow{myDB}% create second row
\DTLnewdbentry{myDB}{key1}{y}

\DTLforeach{myDB}{}{%
\ifnum\value{DTLrowi}=1
\DTLappendtorow{key2}{xprime}% This needs to be in the first row
\fi
}

\DTLdisplaydb{myDB}
\end{document}
share|improve this answer
    
Wow I guess I am rubbing off on you. I thought of doing this, but felt it was too much of a hack, even for me. :-) But it does work.. –  Peter Grill Nov 19 '12 at 19:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.