2
votes

I can quickly create a dictionary in julia via:

julia> d = ['a' => 1, 'b' => 2 ]
Dict{Char,Int64} with 2 entries:
  'b' => 2
  'a' => 1

Now, I want the backslash character as a key in my dictionary. This results in an error because it will escape the '. Instead, I try to escape the \ with another. This doesn't cause an error, but doens't give me the correct result either.

julia> d = ['\\' => 3]
Dict{Char,Int64} with 1 entry:
  '\\' => 3

What am I missing?

1
Also note that this is now deprecated syntax. WARNING: deprecated syntax "[a=>b, ...]". Use "Dict(a=>b, ...)" instead. - rickhg12hs
Thanks for the comment. How would you use a dict comprehension with this new syntax? - cantdutchthis
Interestingly, Dict comprehensions are not deprecated. - rickhg12hs

1 Answers

4
votes

You are getting the correct value. Julia generally tries to show values in the same way that you type them, whereas it will print them in their "canonical" form (without special syntaxes).

julia> show('\\')
'\\'
julia> print('\\')
\
julia> hex('\\')
"5c"
shell> man ascii | grep 5c
     58  X    59  Y    5a  Z    5b  [    5c  \    5d  ]    5e  ^    5f  _