6

I want to convert a space separated list of items, given as argument to a function, into a clist variable in order to iterate over them. However, I am not able to create a usable clist variable as the following code example demonstrates. \clist_map_inline:Nn always takes the whole list instead of separating the elements. What am I doing wrong?

\documentclass{minimal}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new:Nn\parselist:n{
  \tl_set:Nx\l_csv_tl{#1}
  \tl_replace_all:Nnn\l_csv_tl{~}{,}
  \clist_set:Nx\l_csv_clist\l_csv_tl
  \clist_map_inline:Nn\l_csv_clist{
    \noindent Name:~#1\par
  }
}
\ExplSyntaxOff

\begin{document}
\def\csv{fred jane albert}
\ExplSyntaxOn
  \parselist:n\csv
  \parselist:n{john~peter~linda}
\ExplSyntaxOff
\end{document}
AlexG
  • 54,894
  • 1
    You must use Name:~##1. With #1 you are getting the outer argument. – Ulrike Fischer Dec 02 '11 at 11:41
  • 1
    Unless you need a clist for other reasons, I'd use a sequence and \seq_set_split:Nnn here. – Joseph Wright Dec 02 '11 at 12:43
  • Also, you are slightly mis-behaving here with the arg spec. I'd say your first use of \parselist:n should be either a V or N function (little in it here). – Joseph Wright Dec 02 '11 at 12:44
  • @Joseph Wright: Thanks for mentioning the sequence data type. I like the suggested \seq_set_split:Nnn very much, but is it stable enough to be used in a public package? – AlexG Dec 02 '11 at 13:21
  • @AlexanderGrahn One for LaTeX-L! I'd say that if we add something 'experimental', what will get it made 'non-experimental' is good use cases. So give feedback on what you use and we can move them. I'll alter the status of \seq_set_split:Nnn over the next few days: it's definitely useful. – Joseph Wright Dec 02 '11 at 13:58
  • @JosephWright: Given that Alexander uses \tl_set:Nx, the function should probably be called \parselist:x. I think \seq_set_split:Nnn is stable enough (at the implementation level) to be moved to non-experimental. Decide. – Bruno Le Floch Dec 02 '11 at 19:52

1 Answers1

5

I found out the error myself. Using ##1 in the function body of \clist_map_inline:Nn does the trick. Otherwise the original argument as given to \parselist:n is used.

AlexG
  • 54,894