Lists
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 8 cards in this section, with answers
-
set cells {} foreach c [get_cells *] { lappend cells [get_name $c] }Appends each name as one element, keeping the list structure intact.
Hint: Ask what lappend does that set with a concatenation would not.
A list is not just text with spaces:
lappendquotes an element that contains spaces or brackets so it stays a single element. Building a list by string concatenation breaks as soon as a name contains a bracket, which hierarchical names often do. Tcl reference -
set corners {slow typical fast} set first [lindex $corners 0] set last [lindex $corners end]Indexes from 0, and `end` names the last element.
Hint: Note where counting starts, and the word used for the other end.
endand arithmetic on it, such asend-1, work in every list command that takes an index.llengthgives the count, andlrangetakes a span, solrange $corners 1 enddrops the first element. Tcl reference -
lsort -real -decreasing $slacksSorts numerically and largest first, rather than as text ascending.
Hint: Ask what the default comparison would do to numbers.
Sorting defaults to text order, where 10 comes before 9.
-realand-integercompare numerically,-uniqueremoves duplicates, and-commandsorts with your own procedure, which is how a list of objects is sorted by a property. Tcl reference -
lassign [split $line ":"] rule value set parts [split "metal1 metal2 metal3"]Assigns list elements to named variables, and splits text into a list.
Hint: One takes a list apart into variables; the other makes a list from text.
splitcuts text at every character of the separator set, defaulting to whitespace;joinis its inverse.lassignunpacks a list positionally, which is the readable way to take apart a line already parsed into fields. Tcl reference -
set corners {slow typical fast} lsearch $corners typical lsearch -exact -not -inline -all $corners typicalThe position of a match, or minus one.
Hint: The plain form answers where, not whether.
The plain call returns an index, so a test reads
if {[lsearch $l $x] >= 0}. Options change both the matching and the result:-exact,-glob, and-regexpchoose how to match,-allreturns every index, and-inlinereturns the elements instead. Tcl reference -
set corners {slow typical fast} lreplace $corners 1 1 nominal puts $cornersThe original is unchanged, since the command returns a new list.
Hint: Ask what the last line prints.
Every list command except
lappendandlsetreturns a value and leaves its argument alone, so the result has to be assigned. Forgetting this is why a filtered list comes out identical to the one it was built from. Tcl reference -
join {metal1 metal2 metal3} ", " concat {a b} {c d}The first makes one string with a separator; the second merges lists.
Hint: One produces a string, the other a list.
Joining is how a report line or a comma-separated argument is built, and
splitis its inverse. Merging produces a flat list, which is the difference fromlappend, since appending a list adds it as one element. Tcl reference -
set pin {dpath/a_reg[9]/D} report_slack $pin eval "report_slack $pin"The second substitutes the name a second time, so the brackets run as a command.
Hint: One of the two lines lets the brackets be seen by the interpreter again.
Substitution happens once per evaluation, so passing a variable directly is safe however strange the name is. Building a command as a string and evaluating it exposes every special character again. Where a command must be built,
listquotes each part correctly. Tcl reference