
데이터베이스에 있는 특정 열의 다른 값을 모두 얻으려면 어떻게 해야 합니까 datatool
? 예는 데이터베이스로 읽어온 다음 테이블입니다 datatool
.
Name,Town,Age
Adam,Xcity,20
Berta,Ytown,30
Cesar,Ztington,40
Dora,Ztington,20
Emil,Ytown,30
Franz,Ytown,20
Xcity, Ytown,Ztington
이제 해당 데이터베이스에 있는 모든 도시( )의 목록과 중복 없이 해당 데이터베이스에 있는 모든 연령( )의 목록을 가져오고 싶습니다. 매크로/무엇이든 나중에 20,30,40
재사용할 수 있도록 저장할 수 있습니다 .datatool
pgffor
답변1
datatool
요소가 주어진 쉼표로 구분된 목록에 있는지 확인할 수 있는 매크로가 있으므로 \DTLifinlist
다음과 같이 모든 도시 또는 연령의 목록을 구성할 때 사용할 수 있습니다.
\documentclass{article}
\usepackage{etoolbox}
\usepackage{datatool}
\begin{filecontents}{test.csv}
Name,Town,Age
Adam,Xcity,20
Berta,Ytown,30
Cesar,Ztington,40
Dora,Ztington,20
Emil,Ytown,30
Franz,Ytown,20
\end{filecontents}
\DTLloaddb{data}{test.csv}
\begin{document}
\newcommand*{\uniquetowns}{}
\newcommand*{\uniqueages}{}
\DTLforeach*{data}{\Town=Town,\Age=Age}{%
\expandafter\DTLifinlist\expandafter{\Town}{\uniquetowns}%
{}% do nothing, already in list
{% add to list
\ifdefempty{\uniquetowns}%
{\let\uniquetowns\Town}% first element of list
{% append to list
\eappto\uniquetowns{,\Town}%
}%
}%
% Similarly for age
\expandafter\DTLifinlist\expandafter{\Age}{\uniqueages}%
{}% do nothing, already in list
{% add to list
\ifdefempty{\uniqueages}%
{\let\uniqueages\Age}% first element of list
{% append to list
\eappto\uniqueages{,\Age}%
}%
}%
}
List of unique towns: \uniquetowns.
List of unique ages: \uniqueages.
\end{document}
결과: