I have a list of 2-character strings that is read from table, e.g.
\begin{filecontents*}{attributetools.dat}
Attribute
SL
SK
MC
SL
AA
PT
SK
UP
MC
\end{filecontents*}
I want to create a string that will contain unique 2-character strings. The result should be
SLSKMCAAPTUP
Here is what I've come up so far.
\documentclass[10pt,a4paper]{article}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\def\mystring{} % initialize
\def\extendmystring#1#2{\edef\mystring{#1\mystring#2}}
\begin{filecontents*}{attributetools.dat}
Attribute
SL
SK
MC
SL
AA
PT
SK
UP
MC
\end{filecontents*}
\begin{document}
\mystring{}
\pgfplotstableread[columns={Attribute}]{attributetools.dat}\datatableA
\pgfplotstablegetrowsof{\datatableA}
\pgfmathtruncatemacro{\RowsInTable}{\pgfplotsretval-1}
\foreach \k in {0,...,\RowsInTable}{
\pgfplotstablegetelem{\k}{Attribute}\of{\datatableA}
\def\tempstring{\pgfplotsretval}
\extendmystring{}{\tempstring}
}
Display concatenated string
\mystring
\end{document}
but mystring value is empty after each iteration.



\foreachexecutes the loop code inside two groups, so assignments have to either escape that group or be global. The way your\extendmystringmacro is designed you can prefix it with\global(ie,\global\extendmystring{}{\tempstring}) and it will work. – Phelype Oleinik May 05 '19 at 22:47