[Tex/LaTex] Iterating through two lists

comma-separated listloops

I have two comma delimited lists:

ListA(a,b,c,d,e,f,g)

and

ListB(name1,name2,name3,name4,name5)

I would like to iterate through both lists at the same time, something like:

loop
   readvalue from list A
   readvalue from list B
   create new variable aname1{}
repeat

Iteration should stop, if it reaches to the end of either lists.

Best Answer

A solution which goes through all elements:

\documentclass{article}
\usepackage{pstricks}
\psforeach{\A}{a,b,c,d,e,f,g}{%
  \psforeach{\B}{name1,name2,name3,name4,name5}{%
    \expandafter\xdef\csname\A\B\endcsname{Def: \A,\B}}}

\begin{document}
\csname aname1\endcsname

\csname ename4\endcsname
\end{document}

Make sure comma isn't active, otherwise this won't work. For example, the following fails. Also, make sure there are no spurious spaces in your lists.

\begingroup
\catcode`\,=13
\def\x{\endgroup
  \psforeach{\A}{a,b,c,d,e,f,g}{%
    \psforeach{\B}{name1,name2,name3,name4,name5}{%
      \expandafter\xdef\csname\A\B\endcsname{Def: \A,\B}%
    }%
  }%
}
\x