# foreach and renewcommand

\documentclass{article}
\usepackage{pgffor}
\newcommand\tests{
\newcommand\mytext{aaa}
\foreach \i in {bbb,ccc,ddd,eee,fff}{
\renewcommand\mytext\i
}
\mytext
}
\begin{document}
\tests
\end{document}\


It output string "aaa", not "fff"!

The \foreach loop implicitly sets a group. Therefore, the redefinition is lost in the next loop or after the loop. A global definition helps, e.g.:

\documentclass{article}
\usepackage{pgffor}
\newcommand\tests{
\newcommand\mytext{aaa}
\foreach \i in {bbb,ccc,ddd,eee,fff}{
\global\let\mytext\i
}
\mytext
}
\begin{document}
\tests
\end{document}