-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionaries.jl
55 lines (40 loc) · 1.71 KB
/
dictionaries.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Printf
function dictionary_examples()
println("--------------------------------------")
println("Dictionary examples")
println()
println("Dictionaries are key/value pairs where the key must be unique")
println("REMEMBER TO CHECK THE SYMBOLS.JL FILE! SYMBOLS ARE FREQUENTLY USED AS KEYS FOR DICTIONARIES!!!")
dictionary_1 = Dict(1 => "one", 2 => "two")
println("We can display the whole dictionary")
println(dictionary_1)
println("Or we can display a particular value corresponding to the key")
println(dictionary_1[2])
println("Dictionary keys can be any time, for example strings")
dictionary_2 = Dict("pi" => 3.1415, "e" => 2.718)
println(dictionary_2)
println("And we can still get items by key")
println(dictionary_2["pi"])
println("We can also add new items")
dictionary_2["foobar"] = 123.456
println(dictionary_2)
println("And we can delete items")
delete!(dictionary_2, "pi")
println(dictionary_2)
println("We can check to see if a key exists, in this case 'foobar'")
println(haskey(dictionary_2, "foobar"))
println("We can display all keys")
println(keys(dictionary_2))
println("We can display all values")
println(values(dictionary_2))
println("We can also check to see if a particular key/value pair exists in the dictionary")
println(in("foobar"=>123.456, dictionary_2))
println("We can loop through all the key/value pairs")
for key_value in dictionary_2
println(key_value)
end
println("We can also loop through getting the key/value individually")
for(key, value) in dictionary_2
println(key, " : ", value)
end
end