Debian Codegraph @

texmf-dist/doc/latex/datatool/datatool-user-examples/datatool-user-example087.tex

raw
1 % This file is embedded in datatool-user.pdf version 3.4.3 2025-12-04
2 % Example 87 Iterating Over Rows with \DTLmapdata to Append a Column
3 % Label: "ex:mapdataedit"
4 % arara: pdflatex
5 % arara: pdfcrop
6 \documentclass[12pt]{article}
7 \pagestyle{empty}
8 \begin{filecontents}[noheader,overwrite]{studentmarks.csv}
9 Surname,Forename,StudentNo,Assign1,Assign2,Assign3
10 "Smith, Jr",John,102689,68,57,72
11 "Brown",Jane,102647,75,84,80
12 "Brown",Jane,102646,64,92,79
13 "Brown",Andy,103569,42,52,54
14 "Adams",Zoƫ,105987,52,48,57
15 "Brady",Roger,106872,68,60,62
16 "Verdon",Clare,104356,45,50,48
17 \end{filecontents}
18
19 \usepackage{datatool}
20 \DTLsetup{store-datum,default-name=marks}
21 \DTLread{studentmarks.csv}
22 \begin{document}
23 \DTLaction[key=Average]{add column}
24 \DTLmapdata[allow-edits]{%
25 % an open-ended range may be used as the new column is currently missing from this row:
26 \DTLaction[
27 keys={Assign1-},
28 options={mean},
29 datum={round=1},% round the result
30 return={\Mean=mean}
31 ]
32 {row aggregate}
33 \DTLifnull{\Mean}% test the return value
34 {}% row aggregate failed!
35 {% average calculated successfully
36 \DTLsetentry{key=Average,expand-value=\Mean}
37 }
38 }
39
40 \DTLaction[assign={{Average=desc}}]{sort}
41 \DTLaction{display}
42 \end{document}