Dictionaries and arrays
Each card shows a command to explain. A missed card returns later in the round and starts the schedule over. A card answered correctly moves up the schedule and next appears after 1, 3, 7, then 21 days. Keys 1 to 4 choose an option, h shows a hint, and Enter moves on. A hint keeps the card where it is on the schedule. An ordering card is answered by clicking its items in order, and one wrong click ends it. Time values are in nanoseconds. Progress is stored in this browser.
All 6 cards in this section, with answers
-
dict set metrics wns -0.06 dict set metrics tns -2.18 puts [dict get $metrics wns]A dictionary, which is a value held in an ordinary variable.
Hint: Ask what the value of the variable metrics actually is.
A dictionary is a list of alternating keys and values. Because it is a value, it can be passed to a procedure, returned, and stored in a list, which an array cannot.
dict foriterates it, anddict existstests a key without the error thatdict getwould raise. Tcl reference -
set area(nand2) 0.798 foreach cell [array names area] { puts "$cell $area($cell)" }An array, which stays in the interpreter and cannot be passed by value.
Hint: Compare it with a dictionary: ask what you cannot do with this one.
Arrays predate dictionaries and are still everywhere in tool scripts. Passing one to a procedure needs
upvaron its name.array nameslists the keys, in no defined order, andarray getflattens it into a list that a dictionary can be built from. Tcl reference -
dict for {key value} $metrics { puts [format "%-12s %s" $key $value] } dict exists $metrics wns dict keys $metricsWalks every pair in insertion order, with the keys available separately.
Hint: Ask what order the pairs come out in.
Insertion order is guaranteed, which is what makes a dictionary good for a report whose rows should stay in the order they were built. Testing with
dict existsavoids the error that fetching a missing key raises. Tcl reference -
set metrics [dict create wns -0.06] set area(nand2) 0.798A dictionary is a value; an array is a variable.
Hint: Ask which of the two can be returned from a procedure.
Because an array is a collection of variables, a procedure has to reach it with
upvaror pass it asarray get. A dictionary is an ordinary string, so it can be passed, returned, and nested. Arrays remain useful for very large tables that are updated in place. Tcl reference -
dict set runs base place wns -0.06 dict get $runs base place wnsA path of keys into a dictionary of dictionaries.
Hint: Count the words between the command and the value.
Intermediate levels are created as needed on a set, which makes this a tidy way to collect results per run and per stage. Reading a path that does not exist raises an error, so
dict existstakes a path too. Tcl reference -
dict incr counts $type dict lappend byType $type $violationCount into a key, and collect a list under a key.
Hint: Both work on a key that may not be there yet.
This is the whole of a violation summary: walk the report, increment by type, and keep the instances under the same key. Neither needs the key to exist first, which removes the usual test before each update. Tcl reference