How to get value from a list by the index

etoolboxtex-corexstring

I build a list by a macro to store some values. I wonder if there is method to get value by its index number from the list. The following code shows my intention.

Code example:

\documentclass{article}
\def\mylist{a,bc,efg,hijk,lmn,} %define a macro to stroe a series of values.
\newcommand\test[2]{...}% #1 for index number; #2 is a list.
\test{2}{\mylist} % "bc" expected.
\test{5}{\mylist} % "lmn" expected.
\test{6}{\mylist} % get nothing.
\end{document}

Best Answer

For example with expl3:

\documentclass{article}
\ExplSyntaxOn
\seq_new:N\mylist
\seq_set_from_clist:Nn\mylist{a,bc,efg,hijk,lmn,}
\newcommand\test[2]{\seq_item:Nn#2{#1}}
\ExplSyntaxOff
\begin{document}
\test{2}{\mylist} % "bc" expected.
\test{5}{\mylist} % "lmn" expected.
\test{6}{\mylist} % get nothing.
\end{document}
Related Question