\findtoken is a macro that finds the index (starting at zero) of (the full expansion of) its parameter #1 in a predefined list ("a", "b", "c", "d");
it stops compilation if this expansion isn't in the list.
It seems to work fine for typesetting this index,
but I'd like to be able to also assign this number to a \count,
such that \newcount\mycount \mycount=\findtoken{c}\relax \the\mycount
would typeset "2".
I've tried many combinations of \expandafter in the assignment, and
I've read Why does the TeX scanner process tokens for register numbers and macro names differently?
and How to write a TeX macro that accepts a number or a \count register as argument? ,
but couldn't come up with a way of doing that.
So, is it possible?
\documentclass[margin=2mm]{standalone}
\usepackage[T1]{fontenc}
\makeatletter
\newcount\mycount
\newcommand*{\findtoken}[1]{%
% input (#1): a b c d <anything else>
% output: 0 1 2 3 error
\begingroup % protect scratch macros
\edef@tempa{\expanded{#1}}%
@tempswafalse % did anything match?
@tempcnta=\z@ % index of the list
% loop
@for@tempb:={a,b,c,d}\do{%
% if they match, record the current index
\ifx@tempb@tempa
@tempswatrue
\expandafter\aftergroup\the@tempcnta
\fi
\advance@tempcnta@ne
}% end of loop
\if@tempswa\else
\STOPCOMPILATION
\fi
\endgroup
}
\makeatother
\begin{document}
\def\lettera{a}
Checking whether \string\findtoken\ works:
\findtoken{\lettera},
\findtoken{a},
\findtoken{b},
\findtoken{c},
\findtoken{d}.
% uncomment the next line to stop compilation:
% \findtoken{x}
% now, trying to assign \findtoken's expansion into \mycount:
% \mycount\findtoken{c}\relax
% ^ what to write above?! ^
\string\mycount's value is: \the\mycount.
\end{document}


\findtokenis not expandable, it involves\begingroup,\edef, ..... so no amount of\expandafterwill produce a number. – David Carlisle Nov 21 '22 at 17:03