# Why does this simple \ifx test fail?

This should be easy, but I don't understand why the test below fails... I expected Hurrah! to be printed when the \currentchar count reaches 90 (i.e. the character code of Z).

There must be some expansion problem somewhere, but I believe I use \expandafter as is necessary, here. What am I doing wrong? Should I take a different approach?

\def\mysteryletter{Z}

% loop through A-Z to find out the mystery letter
\newcount\currentchar
\currentchar=65 % <-- charcode of A
\loop
\edef\temp{\char\currentchar}
\temp
\expandafter\expandafter\expandafter\ifx\expandafter\mysteryletter\temp Hurrah!\fi
\unless\ifnum \currentchar>90
\repeat

\bye


> \show\temp


and you will see that the \edef has made no difference, neither \char nor \currentchar are expandable, so \temp consists of those two tokens every time, and never is \ifx equal to a character token.

luatex has an expandable \Uchar primitive.

In this case it is easier to test the character code rather than the token.

\def\mysteryletter{Z}

% loop through A-Z to find out the mystery letter
\newcount\currentchar
\currentchar=65 % <-- charcode of A
\loop
\char\currentchar
\expandafter\ifnum\expandafter\mysteryletter=\currentchar Hurrah!\fi
\unless\ifnum \currentchar>90
\repeat

\bye

• Thanks David. Is there any way around using LuaTeX? – jub0bs Feb 11 '14 at 11:12
• @Jubobs do you mean a way using luatex or a way to avoid using luatex (the answer is yes either way) – David Carlisle Feb 11 '14 at 11:13
• I mean a way to avoid using luatex. And what would be the way? :) – jub0bs Feb 11 '14 at 11:14
• @Jubobs updated – David Carlisle Feb 11 '14 at 11:17
• Great stuff! Thanks again. I'll remember to use \show next time... – jub0bs Feb 11 '14 at 11:19
\chardef\mysteryletter=Z
% loop through A-Z to find out the mystery letter
\newcount\currentchar

\currentchar=A
\loop
\chardef\temp=\the\currentchar
\temp
\ifx\mysteryletter\temp Hurrah!\fi
`