Hey! So i was trying to have a sum variable and then iterate over an array of lines and add the result of each line to the sum variable. I found out there are no global variables, but is there any way to do this elegantly without iterating twice over the lines object (which would also require e.g. the same if statement for line.type)?
See the commented out lines for what I want to do:
#let sum = 0
#table(
columns: (auto, auto, 1fr, auto, auto),
align: (col, row) => (auto, auto, auto, auto, auto,).at(col),
inset: 7pt,
stroke: luma(180),
table.header[Amount][Unit][Work Done][Unit Price][Total Price]
..lines.map(line => {
if (line.type == lineTypes.timed) {
let start_time = line.details.time.at(0)
let end_time = line.details.time.at(1)
let hours = (end_time.at(0) - start_time.at(0)) + (end_time.at(1) - start_time.at(1))/60
(
format(hours, 2),
"hours",
line.details.description,
eurStr(HOURLY_RATE),
eurStr(hours * HOURLY_RATE)
)
//sum += hours * HOURLY_RATE
} else if (line.type == lineTypes.special) {
(
str(line.details.amount),
str(line.details.unit),
line.details.description,
eurStr(line.details.pricePerUnit),
eurStr(line.details.amount * line.details.pricePerUnit)
//sum += line.details.amount * line.details.pricePerUnit
)
}
}).flatten(),
[], [], [], "Total:", text(size: 15pt)[#strong(eurStr(sum))]
)
This is what an example of the lines variable looks like:
#let lineTypes = (timed: 0, special: 1)
#let lines = (
(type: lineTypes.timed, details: (
date: "20.09.2024",
time: ((9, 30), (11, 23)),
description: "Driving"
)),
(type: lineTypes.special, details: (
amount: 1, unit: "-", pricePerUnit: 8.0,
date: "24.06.2024",
description: "Using Car"
)),
)
Also p.s. if there’s anything ugly or bad about what I’m doing here, please tell me. I’m pretty new to typst.