Datatool - Fill Variable (name in first row) with value of second row

Is something like this, with the readarray package, sufficient?

REVISION

\documentclass{article}
\usepackage{readarray}[2016-11-07]
\usepackage{filecontents}
\begin{filecontents*}{myfile.dat}
First, Last, Birthday 
John, Doe, 01/01/68 
\end{filecontents*}
\newcounter{mycount}
\newcommand\assigndat[1]{%
  \readarraysepchar{,}%
  \readdef{#1}\mydata%
  \readarray*\mydata\myarray[-,\ncols]%
  \setcounter{mycount}{0}%
  \whiledo{\themycount<\ncols}{%
    \stepcounter{mycount}%
    \edef\tmp{\myarray[1,\themycount]}%
    \expandafter\expandafter\expandafter\xdef\expandafter\csname\tmp\endcsname{%
      \myarray[2,\themycount]}%
    }%
}
\begin{document}
\assigndat{myfile.dat}
\First

\Last

\Birthday
\end{document}

enter image description here


ORIGINAL ANSWER

\documentclass{article}
\usepackage{readarray}[2016-11-07]
\usepackage{filecontents}
\begin{filecontents*}{myfile}
First, Last, Birthday 
John, Doe, 01/01/68 
\end{filecontents*}
\newcommand\assigndat[1]{%
  \edef\tmp{\myarray[1,#1]}
  \expandafter\expandafter\expandafter\def\expandafter\csname\tmp\endcsname{\myarray[2,#1]}
}
\begin{document}
\readarraysepchar{,}
\readdef{myfile.tex}\mydata
\readarray*\mydata\myarray[-,\ncols]
Column 3 title is ``\myarray[1,3]''.

Column 3 data is ``\myarray[2,3]''

\assigndat{1}\First

\assigndat{2}\Last

\assigndat{3}\Birthday
\end{document}

enter image description here


(Rewritten answer now the question is clearer.)

The problem with

\DTLforeach{Test}{\name=name,\value=value}
{
\newcommand{\name}{\value}
}

Is that \name is repeatedly redefined on each iteration by the assignment. This also just tries to define \name rather than construct a control sequence formed from the definition of \name. The etoolbox package provides ways of defining a command given the name (without the initial backslash). For example, \csdef{Voltage}{14 V} defines the command \Voltage with the replacement text 14 V. In this case, you want the control sequence name obtained from \name and the replacement text obtained from the definition of \value, so what you really need is \cslet{\name}{\value}, which creates a new command formed from the definition of \name and assigns it to the definition of \value (so it retains the correct definition after \value is redefined in the next iteration of the loop).

A couple of notes: \value is already defined by the LaTeX kernel (it obtains the value of a counter), so its better to use another command name (say, \Value) to avoid breaking anything. You also need to trim the spurious space from the data.

Updated MWE:

\documentclass{scrartcl}

\usepackage{filecontents}

\begin{filecontents*}{Unnamed1.csv}
name,value
Voltage,14 V
Current,1 A
MoreText,something more is written here
Date,2017/03/08
\end{filecontents*}

\usepackage[ngerman]{babel}
\usepackage{datatool}
\DTLsetseparator{,}
\DTLloaddb{Test}{Unnamed1.csv}
\begin{document}

\DTLforeach{Test}{\Name=name,\Value=value}
{%
 \cslet{\Name}{\Value}%
}

Voltage: \Voltage.

Current: \Current.

MoreText: \MoreText.

Date: \Date.

\end{document}

Voltage: 14 V. Current: 1 A. MoreText: something more is written here. Date: 2017/03/08.

Tags:

Datatool