hledger
hledger copied to clipboard
Nested if statements for better CSV import
I did not find a way to make a conjunction of multiple conditions in CSV import documentation nor examples.
Small example:
- Director's fees are classified separately from wages, but director's reimbursements are treated the same as other emps:
if
payroll
account2 expenses:operating:payroll:wages
if
director
account2 expenses:operating:payroll:directors-fees
if
reimbursement
account2 expenses:operating:education
Documentation:
In order to make a conjunction of conditions you may nest if statements with indentation.
For example when director's fees are classified separately from wages, but director's reimbursements are treated the same as other emps...
This allows you to conveniently build nested matching rules.
Hi @mgajda, since 1.19 you can combine matchers with &: https://hledger.org/csv.html#combining-matchers .
Combining matchers into an AND operation is not the same thing as being able to nest them.
I believe Michal's case could be coded pretty easily using a single flat level of matches (with or without and AND matcher) but the order of operations becomes critical and off the top of my head I'm not sure what it is and I don't see it skimming the docs. Are rules guaranteed to match in a certain order?
@alerque yes rules are evaluated in the order you'd expect (related: https://hledger.org/csv.html#how-csv-rules-are-evaluated). And you're right, @mgajda doesn't need nesting or ANDing here, this would do:
if payroll
account2 expenses:operating:payroll:wages
# more specific rule overriding the above:
if director
account2 expenses:operating:payroll:directors-fees
Nesting if blocks is unlikely to get implemented I suspect.
Yes, I upgraded to latest hledger from GitHub and use & conjunction as a workaround. However, it is somewhat inconvenient when ruleset grows.
Could you give an example ?
If "Nesting if blocks is unlikely to get implemented", could we close this issue?