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.

I am certain this is patently obvious, but the solution is eluding me...

Given a dataset stored in CSV:

ID,    col1,  col2, col3
KEY-1, foo    bar,  baz
KEY-2, alice, bob,  chuck

I would like to define a macro \getCol3{KEY-2} that returns chuck. I am getting tripped up by what appears to be internal key structures (and the fact that I just started using datatool).

\DTLgetvalue{\thevalue}{data}{\therow}{\thecol}

is what I assumed was in the right direction, but I cannot construct valid values for \therow and \thevalue.

share|improve this question

1 Answer 1

You need to use \DTLgetvalueforkey

\begin{filecontents*}{demo.csv}
ID,    col1,  col2, col3
KEY-1, foo,   bar,  baz
KEY-2, alice, bob,  chuck
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\DTLloaddb{data}{demo.csv}
\newcommand*{\thevalue}{}
\newcommand*{\getCol}[2]{%
  \DTLgetvalueforkey{\thevalue}{col#1}{data}{ID}{#2}%
}
\getCol{3}{KEY-2}
\show\thevalue

Here, we are using ID as the reference key to find the appropriate row, which is found using the argument #2.

share|improve this answer
1  
of course, i was missing \newcommand*{\thevalue}{} ... i knew is was something simple. thanks! –  equalspeterd Jan 12 '12 at 22:42

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.