18

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?

enter image description here

\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
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye
jub0bs
  • 58,916

2 Answers2

19

add

> \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
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye
jub0bs
  • 58,916
David Carlisle
  • 757,742
13
\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
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye