Loop over a list with delimiter different from comma

I am trying to iterate over a list, the delimiter of which is not a comma. The reason for not using commas as delimiters is that they are part of the payload. Is there a way to iterate over a list in which the delimiter is a semicolon (or any other delimiter)?

So far, I have tried

\documentclass{article}

\makeatletter
\newcommand{\ls}[1]{\@for\tmp:=#1 \do{\tmp}}
\makeatother

\begin{document}

\ls{(a,b);(c,d)}

\end{document}


Hence, I'd like to loop over the tuples separated by the semicolon, i.e. (a,b) and (c,d).

Edit: I was also trying to find some documentation on \@for, but I was not able to find any.

• See the loop macros in etoolbox – daleif Feb 20 '15 at 13:07
• @daleif would you like to turn your comment into a proper answer? I was writing one, when I saw your comment, so I decided to ask first :) – Gonzalo Medina Feb 20 '15 at 13:15
• \@for is part of the latex format so texdoc source2e If you want to use \@for and a comma you can hide inner commas in {} so \ls{{a,b},{c,d}} would iterate over the two pairs – David Carlisle Feb 20 '15 at 13:35

The etoolbox has many interesting features, including making list parsers. See also the unstarred version.

\documentclass[a4paper]{memoir}
\usepackage{etoolbox}
\begin{document}
\newcommand\handler[1]{item: #1\par}
\DeclareListParser*\forsemicolonlist{;}
\forsemicolonlist\handler{(a,b);(c,d);(e,f)}
\end{document}


For example, you can use this code:

\def\ls#1{\lsA#1;;}
\def\lsA#1;{\ifx;#1;\else \dosomething{#1}\expandafter\lsA\fi}
\def\dosomething#1{\message{I am doing something with #1}}

\ls{(a,b);(c,d);(e,f)}