Finding Item in Dict

I’m trying to add a new value to data tables in a dictionary, but when I run this.

using DataFrames

                           
general_ledger=Dict(
    "dividends" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    "expenses" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    "assets" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    "liabilities" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    "equity" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    "revenue" => DataFrame(
        date=Vector{String}(),
        debit=Vector{Int64}(),
        credit=Vector{Int64}()),
    )
    
function ledgertransaction!(
    ledger,
    ledger_name,
    date,
    credit_account,
    credit_amount,
    debit_account,
    debit_amount
)
    if length(date) == 0
        return (false,"Error -- no date entered")
    elseif !( credit_account in names(ledger) )
        return (false, "Error -- credit account not found.")
    elseif !( debit_account in names(ledger) )
        return (false, "Error -- debit account not found")
    elseif credit_amount-debit_amount != 0          
        return (false,"Error -- credit and debit must match." )
    elseif credit_account == debit_account
        return (false, "Error -- credit and debit must be different accounts")
    else
        default_values=Dict("date"=>"",credit_account=>0,debit_account=>0)

        spec = Dict(
            "date" => date, 
            credit_account => credit_amount, 
            debit_account => debit_amount)
        row = merge(default_values, spec)
        push!(ledger,row)
    

        return (true, "there  are no errors")   
    end
end

function add2ledger!(
    journal_entry,
    general_ledger,
    ledger_name, 
    date,
    credit_account,
    credit_amount,
    debit_account,
    debit_amount
)
    if !( ledger_name in keys(general_ledger) )
        return (false, "Error -- "*ledger_name*" not found")
    else
        ledger=general_ledger[ledger_name]
        return ledgertransaction!(ledger,ledger_name,date,credit_account,credit_amount,debit_account,debit_amount)
    end
end

#when I try to execute the code, 

add2ledger!(journal_entry,general_ledger,"expenses","12 Jun 2021","debit",100,"credit",100)

I get
(false, Error–credit account not found)

Your code is not an MWE so we can’t tell exactly what’s going on, but it looks like your second condition is triggered:

elseif !(credit_account in names(ledger))

so maybe add @show credit_account, names(ledger) before the error to see what’s going on.

I forgot I posted this. is @show a comand to tests if a key is in a Dict?

Now, @show is a simple macro that prints the name of a variable and its value. You can see it as the simplest debugging tool (it’s a poor man’s "step into the function and see what the value of my variable is at this point).

My suggestion was merely to check what the values of the variables in your ifelse condition are at the point at which it is triggered - it seems that they are not what you expect them to be if you’re surprised by the return value you’re getting.

Yeah, I had the wrong Dict, from switching things around, and I got it to work. But, I’m not used to Dicts, so a diagnosing tool like that, is the kind of things I need to learn.

1 Like