# How to get the last element of a specific column from a CSV file?

I have CSV files of different lengths. For example

a,b,c,d,e
0,0, 5, 5, 1
0,5, 5, 11, 2
0,11, 5, 16, 1
5,0, 10, 6, 1
5,6, 10, 11, 2


I want to get the value of the last element of the c and d columns, for example c=10 and d=11. And every time with a different file of different length. I am using the datatool package.

\DTLloaddb[
keys={a,b,c,d,e}
]{data}{input}
.....
....
\DTLforeach*{data}{\a=a,\b=b,\c=c,\d=d,\e=e}{
......
...
\eappto\mydraws{[gray, fill=\k,line width=0.05cm] (axis cs:\a,\b) rectangle (axis cs:\c,\d);}%
}


I don't know how I can access only the last element of a specific column.

-
Please use the code formatting options on the edit menu marked with the symbol {} or select your code and hit Ctrl+K or manually indent the code lines with four whitespace characters. –  percusse Jan 21 '13 at 16:14

You could just step thru the DB set a global to the appropriate column entry, and at the end it will contain the last entry:

## Code:

\documentclass{article}
\usepackage{datatool}

\usepackage{filecontents}% Comment to prevent overwriting MyData.csv
\begin{filecontents*}{MyData.csv}
a, b,  c,  d, e
0, 0,  5,  5, 1
0, 5,  5, 11, 2
0,11,  5, 16, 1
5, 0, 10,  6, 1
5, 6, 10, 11, 2
\end{filecontents*}

\newcommand*{\LastC}{}%
\newcommand*{\LastD}{}%

\begin{document}