Tutorial by Examples

If we have this dictionary: set alpha {alice {items {}} bob {items {}} claudia {items {}} derek {items {}}} And want to add "fork" and "peanut" to Alice's items, this code won't work: dict lappend alpha alice items fork peanut dict get $alpha alice # => items {} items f...
Creating a dictionary: set mydict [dict create a 1 b 2 c 3 d 4] dict get $mydict b ; # returns 2 set key c set myval [dict get $mydict $key] puts $myval # remove a value dict unset mydict b # set a new value dict set mydict e 5 Dictionary keys can be nested. dict set mycars mustang colo...
set alpha {a 1 b 2 c 3} dict get $alpha b # => 2 dict get $alpha d # (ERROR) key "d" not known in dictionary If dict get is used to retrieve the value of a missing key, an error is raised. To prevent the error, use dict exists: if {[dict exists $alpha $key]} { set result [d...
You can iterate over the contents of a dictionary with dict for, which is similar to foreach: set theDict {abcd {ab cd} bcde {ef gh} cdef {ij kl}} dict for {theKey theValue} $theDict { puts "$theKey -> $theValue" } This produces this output: abcd -> ab cd bcde -> ef gh c...

Page 1 of 1