2

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[
    headers={a,b,c,d,e},
    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.

1
  • 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, 2013 at 16:14

1 Answer 1

4

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:

enter image description here

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}

\DTLloaddb[%
    headers={a,b,c,d,e},%
    keys={a,b,c,d,e}%
]{myDB}{MyData.csv}

\DTLforeach*{myDB}{\CurrentA=a,\CurrentB=b,\CurrentC=c,\CurrentD=d,\CurrentE=e}{%
    \xdef\LastC{\CurrentC}    
    \xdef\LastD{\CurrentD}
} 

LastC = \LastC, and LastD = \LastD
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .