Read variables from csv file based on context

csvcsvsimple

I'm trying to read two variables from a csv file.

function,value
1. pos,some text
2. pos,some other text

This runs before \begin{document} and I wan't the value of 1. pos in \firstvar and the value of 2. pos in \secondvar.
Edit: By value I mean the value-column, not the string 1. pos itself.

\usepackage{csvsimple}

\newcommand{\firstvar}{tmpvalone}
\newcommand{\secondvar}{tmpvaltwo}
\csvreader[head to column names]{file.csv}{}%
{
    \ifstrequal{\function}{1. pos}
    {\renewcommand{\firstvar}{\value}}
    {
        \ifstrequal{\function}{2. pos}
        {\renewcommand{\secondvar}{\value}}
        {}
    }
}

Currently, there are two problems:

  1. \ifstrequal doesn't work. I know it has something to do with the expansion of the commands, but I couldn't get it to work, even with \expandafter\ifstrequal\expandafter.
  2. Even if the comparison would work, the \renewcommand would be empty. I tested this, by using \ifnumequal{\thecsvrow}{x} instead of \ifstrequal.

I feel like, I'm really close, but I don't really understand what I'm doing.

Best Answer

The following code works:

\documentclass{article}

\usepackage{csvsimple}

\begin{filecontents*}[overwrite]{630556.csv}
function,value
1. pos,some text
2. pos,some other text
\end{filecontents*}

\begin{document}

\newcommand{\firstvar}{tmpvalone}
\newcommand{\secondvar}{tmpvaltwo}

\csvreader[head to column names]{630556.csv}{}{
  \ifcsvstrequal{\function}{1. pos}{ \edef\firstvar{\value} } { }
  \ifcsvstrequal{\function}{2. pos}{ \edef\secondvar{\value} }{ }
}

\firstvar \quad \secondvar

\end{document}
Related Question