hledger icon indicating copy to clipboard operation
hledger copied to clipboard

Nested if statements for better CSV import

Open mgajda opened this issue 5 years ago • 6 comments
trafficstars

I did not find a way to make a conjunction of multiple conditions in CSV import documentation nor examples.

Small example:

  1. 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.

mgajda avatar Nov 16 '20 11:11 mgajda

Hi @mgajda, since 1.19 you can combine matchers with &: https://hledger.org/csv.html#combining-matchers .

simonmichael avatar Nov 17 '20 16:11 simonmichael

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 avatar Nov 17 '20 18:11 alerque

@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.

simonmichael avatar Nov 17 '20 19:11 simonmichael

Yes, I upgraded to latest hledger from GitHub and use & conjunction as a workaround. However, it is somewhat inconvenient when ruleset grows.

mgajda avatar Nov 18 '20 13:11 mgajda

Could you give an example ?

simonmichael avatar Nov 18 '20 15:11 simonmichael

If "Nesting if blocks is unlikely to get implemented", could we close this issue?

the-solipsist avatar Dec 21 '21 21:12 the-solipsist