(This is my first question here, sorry for any "side effects" of it...)
I'd like to implement that, depending on the value of \MyR (a number between 0 and 1, 1 excluded), the first/second/... item of a set would be chosen. In the MWE below, a set is realized in the format {item1\or item2\or item3 ...}. If, for example, a set has two elements: {item1\or item2} then, for values of \MyR below 0.5, item1 would be returned, and for values above 0.5, item2 is to be returned. From a set {item1\or item2\or item3}, 0.33 would yield item1 while 0.34 would lead to item2. Below I use \ifcase, and compute which of its branches should be returned.
Version (*) (see below, implementation via \newcommandFromSet) seems to work. Hovewer, the variants (**) and (***) (two attempts for \FromSet) fail when we uncomment the last (but one) line %\fpeval....
The success of (*) seems to justify the basic concept, but how could I cure the definition of \FromSet? Thank you!
(Link to @egreg's \CountToken.)
[A format {item1\SomeShortToken item2\SomeShortToken item3 ...} would be easy to
type, and calculating the number of items would also be for user
convenience.]
\documentclass{article} %% MWE
\usepackage{xfp}
\def\finish{\finish} %% Frequency of token #1 (stored
\def\CountToken[#1,#2]{% %% in counter #2) in what's
\def\TestedToken{#1}% %% between \CountToken[#1,#2]
\ifdefined#2#2=0 \else\newcount#2\fi %% and \finish. Code copied from
\let\TotalOccurrence#2% %% https://tex.stackexchange.com
\let\next\TestNext\next %% questions/525556/token-
} %% counter-strange-behaviour-
\long\def\TestNext#1{% %% of-math-mode (egreg's version)
\def\CurrentToken{#1}% %%
\ifx\CurrentToken\finish %%
\let\next\relax %%
\else %%
\ifx\CurrentToken\TestedToken %%
\advance\TotalOccurrence by 1 %%
\fi %%
\fi %%
\next %%
} %%
\newcommand{\MyR}{0.67} %% my "global variable".
\newcommand{\newcommandFromSet}[2]{\CountToken[\or,\cnti]#2\finish %% ()
\xdef#1{\ifcase\fpeval{floor(\MyR(1+\cnti))} #2\fi}}
\newcommand{\FromSet}[1]{\CountToken[\or,\cnti]#1\finish %% ()
\ifcase\fpeval{floor(\MyR*(1+\cnti))} #1\fi}
%\renewcommand{\FromSet}[1]{\newcommandFromSet{\MyTemp}{#1}\MyTemp} %% (*)
\begin{document}
\newcommandFromSet{\MyText}{first\or second\or third}
\newcommandFromSet{\MyNumber}{11\or 12\or 13}
The \MyText\ number is \MyNumber, I repeat,
\fpeval{1*\MyNumber}. %% everything seems to be good.
The \FromSet{first\or second\or third} number is %% this line seems to work.
\FromSet{11\or 12\or 13}, I repeat, %% this line also seems to work.
%\fpeval{1*\FromSet{\or 11\or 12\or 13}}. %% ! Extra \or. \TestedToken ->\or
\end{document}

\ortokens with code that exploits\ifcase? That's quite likely the source of your problem. I'm quite confident that the second version in the answer you refer to doesn't suffer from the problem. – egreg Jul 18 '20 at 20:05\orand\ifcaseis not necessary (just one seemingly plausible way), and it does work in the form of\newcommandFromSet. Nevertheless, more generally, I would be satisfied with any\SomeShortToken– or, say, a semicolon – in a format{item1\SomeShortToken item2\SomeShortToken item3 ...}. The format itself I find user-friendly, and I would like LaTeX to calculate the number of items (rather than the user). – TFulop Jul 18 '20 at 20:16expl3version: For the users I have in mind, the syntax ofexpl3would be somewhat intimidating, I'm afraid… (I mean, they would also take a look at the preamble.) – TFulop Jul 18 '20 at 20:22\CountToken? – egreg Jul 18 '20 at 20:52expl3… – TFulop Jul 18 '20 at 20:59_, numbers, and other 'exotic' characters are not allowed in command names. Their eyes still need training to learn what syntax is TeX-ish and what is not. A\CountTokenis more consistent with their level, I think. – TFulop Jul 18 '20 at 21:08