Iterating through two lists
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
You can map a function on two comma-separated list, using the code below. It also lets you "zip" two comma separated lists together. All this is expandable, e.g. suitable for use in a \write
statement, etc. Or rather, it is expandable if the function you map is itself expandable. See the end of the code for an example suited to your case.
\documentclass{minimal}
\usepackage{expl3}
\ExplSyntaxOn
% Spaces are now ignored, and `_` and `:` can be used in macro names.
%
% `\tl_if_either_empty_ii:nn` tests whether either one of two token
% lists is empty.
%
\prg_new_conditional:Npnn \tl_if_either_empty_ii:nn #1 #2 {p,T,F,TF} {
\tl_if_empty:nTF {#1} {\prg_return_true:} {
\tl_if_empty:nTF {#2} {\prg_return_true:} {\prg_return_false:}
}
}
% Function to zip two clist together, e.g.,
% {1,2,3,4,5} {aa,bb,cc,d} -> {1}{aa}, {2}{bb}, {3}{cc}, {4}{d}
% It stops when reaching the end of any of the two lists. For people who
% care: it is `f`-expandable.
%
\cs_new:Npn \clist_zip_ii:nn #1 #2 {
\clist_zip_ii_aux:nw {} #1, \q_mark, #2, \q_mark.
}
\cs_new:Npn \clist_zip_ii_aux:nw #1 #2, #3 \q_mark, #4, #5 \q_mark. {
\tl_if_either_empty_ii:nnTF {#3} {#5} {
#1 {#2}{#4}
}{
\clist_zip_ii_aux:nw {#1 {#2}{#4},} #3 \q_mark, #5 \q_mark.
}
}
\cs_generate_variant:Nn \clist_zip_ii:nn {VV}
% To map a function `#3` of two arguments onto the zipped result,
% we do something similar, essentially replacing commas by `#1` in
% the output.
\cs_new:Npn \clist_map_zip_ii:nnN #1 #2 #3 {
\clist_map_zip_ii_aux:Nnw #3 {} #1, \q_mark, #2, \q_mark.
}
\cs_new:Npn \clist_map_zip_ii_aux:Nnw #1 #2 #3, #4 \q_mark, #5, #6 \q_mark. {
\tl_if_either_empty_ii:nnTF {#4} {#6} {
#2 #1{#3}{#5}
}{
\clist_map_zip_ii_aux:Nnw #1 {#2 #1{#3}{#5}} #4 \q_mark, #6 \q_mark.
}
}
\cs_generate_variant:Nn \clist_map_zip_ii:nnN {VV}
% ======================= Your comma separated lists ==================
% All those `g` mean `global`.
\clist_new:N \g_my_first_clist
\clist_new:N \g_my_second_clist
\clist_gput_right:Nn \g_my_first_clist {a,b,c,d,e,f}
\clist_gput_right:Nn \g_my_second_clist {1,2,3,4,5}
\cs_new:Npn \my_create_variable:nn #1 #2 {
\iow_term:n {Creating~variable~``#1 name #2''} % Message to the terminal
\tl_new:c {#1 name #2}
}
\clist_map_zip_ii:VVN \g_my_first_clist
\g_my_second_clist
\my_create_variable:nn
% Restore the usual behaviour of space, colon and underscore.
\ExplSyntaxOff
\begin{document}
\end{document}
If you don't mind global assignments and to interleave the lists you can use the \foreach
macro of pgffor
:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \name/\value in {namea/a,nameb/b,namec/c} {%
\global\expandafter\def\csname\name\expandafter\endcsname\expandafter{\value}%
}
% Test:
\show\namea \show\nameb \show\namec
\end{document}
Otherwise you need to program you own loop which removes a value from each list. Just look e.g. how the \@for
loop is defined in latex.ltx
.