I'm attempting to create 2 macros to simulate an array list as in Java for example. I'm using xstring StrSubstitute for this purpose, but I'm having a problem. Below is the code, it's a bit long as it contains the macros plus the test document.
The currently active StrSubstitute line is working as it is specifying the recipient of the substitution explicitly (\Cities).
The 1st commented StrSubstitute line (using \csname #1\endcsname) on the other hand causes the following TeX error :
! Extra \endcsname.
<argument> \csname testAL\endcsname
l.21 \addElementToArrayList{testAL}{Paris}
The 2nd commented line (using just #1) causes the following error:
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.21 \addElementToArrayList{testAL}{Paris}
I tried to read some TeX resources, but I am not able to make it work and I find lots of possibilities (\def, \edef, \noexpand...)
\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\RequirePackage{xstring} %will use this package for the internal list structure
\def\ARRAYENDSENTINEL{@@} %indicates array end
\def\ARRAYSEPARATOR{,} %element separator
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Macro to create a new list:
%Parameters: <list name>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\newArrayList#1{%
\expandafter\def\csname#1\endcsname{\ARRAYENDSENTINEL}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Macro to append element on a list:
%Parameters: <list name>, <element to add>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\addElementToArrayList#1#2{
\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[\Cities] %working
%\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[\csname #1\endcsname] %NOT working!
%\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[#1] %NOT working either!
}
%opening
\title{Testing ArrayList}
\author{}
\begin{document}
\maketitle
\newArrayList{Cities}
Contents of Cities: \textbf{[\Cities]}
\addElementToArrayList{Cities}{Paris}
Contents of Cities: \textbf{[\Cities]}
\addElementToArrayList{Cities}{London}
Contents of Cities: \textbf{[\Cities]}
\addElementToArrayList{Cities}{Rome}
Contents of Cities: \textbf{[\Cities]}
\end{document}
