The 'classical' way to store values with a key is to use a csname-based approach
\def\addvalue#1#2{\expandafter\gdef\csname my@data@#1\endcsname{#2}}
\def\usevalue#1{\csname my@data@#1\endcsname}
To allow arbitrary material in the key, you can use e-TeX's \detokenize
, and also add a test for undefined values if you wish
\def\addvalue#1#2{\expandafter\gdef\csname my@data@\detokenize{#1}\endcsname{#2}}
\def\usevalue#1{%
\ifcsname my@data@\detokenize{#1}\endcsname
\csname my@data@\detokenize{#1}\expandafter\endcsname
\else
\expandafter\ERROR
\fi
}
This approach needs one csname per key, which can get awkward for very large data sets. It also means you end up doing all of the work yourself every time you want to use such an approach. As has been pointed out, the etoolbox
package offers some support for data storage. I would also point to the LaTeX3 prop
data structure
\usepackage{expl3}
\ExplSyntaxOn
\prop_new:N \g_my_data_prop
\cs_new_protected:Npn \addvalue #1#2 { \prop_gput:Nnn \g_my_data_prop {#1} {#2} }
\cs_new:Npn \usevalue #1 { \prop_get:Nn \g_my_data_prop {#1} }
\ExplSyntaxOff
This approach offers a full range of support functions, for example tests for the presence of a key, counting the total number of keys, and so on.
\do
macro that can process a key-value list.