# Latex equivalent of DECODE (check text and assign number) [closed]

I've four probable sentences in CSV file. Ex: sentence 1 here , one more sentence, one more sentence added, last sentence.

In Latex; I want to print 1 if "sentence 1 here" is present. Something like DECODE in SQL. Can you please point me URL where I can learn similar functionality.

## closed as unclear what you're asking by egreg, user13907, LaRiFaRi, user31729, Joseph Wright♦Oct 21 '14 at 21:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• This question is somewhat unclear – user31729 Sep 21 '14 at 16:12
• I'm sorry, my English is not good. I'm creating tex file dynamically in PHP based on external inputs from CSV file. one of the value in CSV file is 'string of words'; i want to write corresponding number in tex file generated. – Pawan Mude Sep 21 '14 at 16:16
• Could you post a minimal working example (MWE) that illustrates what you'd like to do? Right now, it isn't clear what the connection between the CSV file and the TeX file currently is or is supposed to be. Make a minimal CSV which illustrates the kind the PHP code produces and then a minimal TeX file which shows how you are using the CSV already and what you would like to do with the string. Telling me that you want something like something in SQL is not helping, I'm afraid. – cfr Sep 21 '14 at 16:23

Since you are specifically looking for SQL type functionality I suggest you explore the datatool package. The MWE reads the CSV file MyData.csv and then searches for "one more sentence" and "three more sentences":

## Notes:

• The filecontents package was used to set up a file to read for this test case. It is not needed in you actual use case.
• I have used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.
• Change the "Found it" text to Be just "1" (as per the question) if that is all the functionality that you desire, or adjust the code there to do something else.

## Code:

\documentclass{article}
\usepackage{datatool}
\usepackage{xstring}
\usepackage{etoolbox}

%\usepackage{filecontents}% <-- Commented out to prevent overwriting MyData.csv
\begin{filecontents*}{MyData.csv}
sentence 1 here,
one more sentence,
last sentence,
\end{filecontents*}

\newtoggle{FoundInDB}
\newcommand*{\CheckIfInDB}[4]{%
% #1 = database
% #2 = string to chek
% #3 = code to execute if string is found
\global\togglefalse{FoundInDB}%
\DTLforeach{#1}{%
\CurrentSentence=Sentence%
}{%
\IfStrEq{\CurrentSentence}{#2}{%
%% Found string -- we are done
\global\toggletrue{FoundInDB}%
\dtlbreak% No point in searching rest of file
}{%
% Still haven't found what we are looking for :-(
}%
}%
\iftoggle{FoundInDB}{#3}{#4}%
}%

\begin{document}

\end{document}


If I understand correctly you want to check a row in a CSV file and assign a number based on this sentence.

You can simply achieve this using R and sweave files (they are basically LaTeX files which have R language functionality. Remember to add knitr package to R studio.

So you begin the file as normal latex, then after \begin{document} You write the following chunk

<<echo=false>>=