I'm assigning each dictionary element into one variable. Is there a way to then get the related key/value out of the variable?
This doesn't work:
for letter = Dict("a"=>"A", "b"=>"B", "c"=>"C")
println("$letter[1] upper case is $letter[2]")
end
Output:
"c"=>"C"[1] upper case is "c"=>"C"[2]
"b"=>"B"[1] upper case is "b"=>"B"[2]
"a"=>"A"[1] upper case is "a"=>"A"[2]
I'd like the output to look like this:
"c upper case is C"
"b upper case is B"
"a upper case is A"
I know it can be done using a tuple for the iterated variable but I'd like to use a single variable.
If you really don't want to loop by (key, value) then all you are missing are the brackets after the $ in println:
for letter in Dict("a"=>"A", "b"=>"B", "c"=>"C")
println("$(letter[1]) upper case is $(letter[2])")
end
Out:
c upper case is C
b upper case is B
a upper case is A
I would still recommend looping for (key, value) in my_dict as that's more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With