Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer

up vote 4 down vote accepted

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.