# Finding Item in Dict

**URL:** https://discourse.julialang.org/t/finding-item-in-dict/62814
**Category:** Data
**Created:** [June 12, 2021, 9:19pm UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814 "2021-06-12T21:19:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 12, 2021, 9:19pm UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814/1 "2021-06-12T21:19:41Z")

</div>

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

```julia
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)

```julia

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 13, 2021, 7:41am UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814/2 "2021-06-13T07:41:18Z")

</div>

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:

```julia
elseif !(credit_account in names(ledger))

```

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

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 13, 2021, 7:43am UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814/3 "2021-06-13T07:43:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 13, 2021, 7:45am UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814/4 "2021-06-13T07:45:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 13, 2021, 7:47am UTC](https://discourse.julialang.org/t/finding-item-in-dict/62814/5 "2021-06-13T07:47:48Z")

</div>

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.
