The main error is in the line
\ifnum\select@i=#1\@@select@item\@endfortrue\fi%
When \@@select@item the lookup for the number is not finished and TeX keeps expanding until finding something that's not a digit; when \@@select@item is 1, it's easy to predict chaos. A better style is
\ifnum#1=\select@i\@@select@item\@endfortrue\fi
because \select@i is a counter, so the <number> it represents is complete.
However you're off by one: in case of non numeric input the premature expansion of \@@select@item finds the preceding item.
Here's a working version; in order to avoid problems with expansion following a constant it's better to use \@ne for the constant 1 and \z@ for the constant 0.
\documentclass{article}
\usepackage{xfor}
\makeatletter
\newcount\select@count
\newcount\select@i
\newcommand\select[2]{%
\select@i=\z@\select@count=\z@
\@for\@@select@item:={#2}\do{\advance\select@count by\@ne}%
\ifnum#1>\select@count??\else
\@for\@@select@item:={#2}\do{%
\advance\select@i by\@ne
\ifnum#1<1??\@endfortrue\fi
\ifnum#1=\select@i\@@select@item\@endfortrue\fi
}%
\fi
}
\makeatother
\begin{document}
\begin{itemize}
\item \verb+\select{0}{A,B,C,D,E}+ gives \select{0}{A,B,C,D,E}
\item \verb+\select{1}{A,B,C,D,E}+ gives \select{1}{A,B,C,D,E}
\item \verb+\select{2}{A,B,C,D,E}+ gives \select{2}{A,B,C,D,E}
\item \verb+\select{5}{A,B,C,D,E}+ gives \select{5}{A,B,C,D,E}
\item \verb+\select{10}{A,B,C,D,E}+ gives \select{10}{A,B,C,D,E}
\item \verb+\select{0}{1,2,3,4,5}+ gives \select{0}{1,2,3,4,5}
\item \verb+\select{1}{1,2,3,4,5}+ gives \select{1}{1,2,3,4,5}
\item \verb+\select{2}{1,2,3,4,5}+ gives \select{2}{1,2,3,4,5}
\item \verb+\select{5}{1,2,3,4,5}+ gives \select{5}{1,2,3,4,5}
\item \verb+\select{10}{1,2,3,4,5}+ gives \select{10}{1,2,3,4,5}
\end{itemize}
\end{document}
Note that the initial setting of \select@i is at 0.

An alternative version with only one counter and only one cycle:
\makeatletter
\newcount\select@count
\newcommand\select[2]{%
\ifnum#1<\@ne??\else
\select@count=\z@
\@for\@@select@item:={#2,\@@nil}\do{%
\advance\select@count\@ne
\expandafter\ifx\@@select@item\@@nil
??\@endfortrue
\else
\ifnum#1=\select@count
\@@select@item\@endfortrue
\fi
\fi}%
\fi
}
\makeatother
The inevitable LaTeX3 solution, which has a big advantage on the \@for based ones: it's fully expandable.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand \select { m m }
{
\egreg_select:nn { #1 } { #2 }
}
\cs_new:Npn \egreg_select:nn #1 #2
{
\bool_if:nTF
{
\int_compare_p:n { #1 < 1 }
||
\int_compare_p:n { #1 > \clist_count:n { #2 } }
}
{
??
}
{
\clist_item:nn { #2 } { #1 }
}
}
\ExplSyntaxOff
\begin{document}
\begin{itemize}
\item \verb+\select{0}{A,B,C,D,E}+ gives \select{0}{A,B,C,D,E}
\item \verb+\select{1}{A,B,C,D,E}+ gives \select{1}{A,B,C,D,E}
\item \verb+\select{2}{A,B,C,D,E}+ gives \select{2}{A,B,C,D,E}
\item \verb+\select{5}{A,B,C,D,E}+ gives \select{5}{A,B,C,D,E}
\item \verb+\select{10}{A,B,C,D,E}+ gives \select{10}{A,B,C,D,E}
%these do not work
\item \verb+\select{1}{1,2,3,4,5}+ gives \select{1}{1,2,3,4,5}
\item \verb+\select{2}{1,2,3,4,5}+ gives \select{2}{1,2,3,4,5}
\end{itemize}
% Let's show it's fully expandable!
\edef\x{\select{2}{1,2,3,4,5}}\x
\end{document}

If you want to give a macro for the second argument, you need to expand it before the functions come into action.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand \select { m m }
{
\egreg_select:on { #2 } { #1 }
}
\cs_new:Npn \egreg_select:nn #1 #2
{
\bool_if:nTF
{
\int_compare_p:n { #2 < 1 }
||
\int_compare_p:n { #2 > \clist_count:n { #1 } }
}
{
??
}
{
\clist_item:nn { #1 } { #2 }
}
}
\cs_generate_variant:Nn \egreg_select:nn { o }
\ExplSyntaxOff
\newcommand{\alist}{X,Y,Z}
\begin{document}
\begin{itemize}
\item \verb+\select{0}{A,B,C,D,E}+ gives \select{0}{A,B,C,D,E}
\item \verb+\select{1}{A,B,C,D,E}+ gives \select{1}{A,B,C,D,E}
\item \verb+\select{2}{A,B,C,D,E}+ gives \select{2}{A,B,C,D,E}
\item \verb+\select{5}{A,B,C,D,E}+ gives \select{5}{A,B,C,D,E}
\item \verb+\select{10}{A,B,C,D,E}+ gives \select{10}{A,B,C,D,E}
%these do not work
\item \verb+\select{1}{1,2,3,4,5}+ gives \select{1}{1,2,3,4,5}
\item \verb+\select{2}{1,2,3,4,5}+ gives \select{2}{1,2,3,4,5}
\end{itemize}
\verb+\select{2}{\alist}+ gives \select{2}{\alist}
% Let's show it's fully expandable!
\edef\x{\select{2}{1,2,3,4,5}}\x
\end{document}
Note that for efficiency I inverted the order of the arguments in \egreg_select:nn (the first is the list, the second is the number; but this doesn't change the syntax for the user level command.
texdoc interface3on your machine – egreg Nov 10 '13 at 15:05\clist_item:nninto\clist_item:onand add, just before\ExplSyntaxOff, the magic line\cs_generate_variant:Nn \clist_item:nn { o }– egreg Nov 11 '13 at 12:54\cs_generate_variant:Nn \clist_item:nn { o }
– user39714 Nov 12 '13 at 16:38\cs_new:Npn \dgmn_select:nn #1 #2
{
\bool_if:nTF
{\int_compare_p:n { #1 < 1 } || \int_compare_p:n { #1 > \clist_count:n { #2 }}}
{??}{\clist_item:on { #2 }{ #1 }}
}
\ExplSyntaxOff`