I am probably missing something very obvious. Can someone help me understand this?

I am probably missing something very obvious. Can someone help me understand this?

foo_string = "foo"
foo_dict = Dict("f"=>0, "o"=>0)

this:

haskey(foo_dict, "f")

returns: true

but why does

for char in foo_string
    print(char, " ", haskey(foo_dict, char))
end

returns this?

f false
o false
o false

What is a Julian way to check if a character in a string is in a dictionary key?

Thanks in advance.

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

1 Like
julia> foo_string = "foo"
"foo"

julia> foo_dict = Dict("f"=>0, "o"=>0)
Dict{String,Int64} with 2 entries:
  "f" => 0
  "o" => 0

julia> for char in foo_string
         println("variable has type ",typeof(char))
       end
variable has type Char
variable has type Char
variable has type Char

julia> haskey(foo_dict,'f')
false

julia> typeof('f')
Char

# But if we do this instead

julia> haskey(foo_dict,"f")
true

julia> typeof("f")
String

julia> for char in foo_string
         println(  char, " ", haskey(foo_dict, "$char")  )
       end
f true
o true
o true
1 Like