Strings and patterns
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 7 cards in this section, with answers
-
if { [string match "*_reg*" $name] } { ... }Glob-style matching, where `*` matches any text and `?` one character.
Hint: Ask which pattern language this is: the shell's or a regular expression's.
Glob patterns are the same ones the object-access commands take, which is why
get_cells *_reg*feels familiar. For anything with alternation or capture,regexpis the tool, at the cost of a denser pattern. Tcl reference -
if { [regexp {^(\w+)/(\w+)$} $pin -> inst port] } { ... }Captures the two groups into inst and port, discarding the whole match.
Hint: Three variables follow the pattern. Ask what the first one receives.
The first variable after the string takes the whole match, and
->is the conventional throwaway name for it. The pattern is in braces so Tcl does not substitute the backslashes before the regular-expression engine sees them. Tcl reference -
set out [string map {"/" "_" "\[" "" "\]" ""} $name]Replaces each key with its value, so the name becomes filename-safe.
Hint: Ask what the pairs in the list mean.
This is the quick way to make a hierarchical pin name usable as a file name, without a regular expression. The replacements are applied in one pass, so an earlier pair cannot match text produced by a later one. Tcl reference
-
puts [format "%-30s %8.3f" $endpoint $slack]Formats a padded name and a slack with three decimals, so columns line up.
Hint: Two field specifications. Ask what the numbers before them control.
The minus sign left-justifies, the first number is the field width, and the second is the precision. Reports that line up are read far faster than ones that do not, which matters when a script's output is scanned by eye across hundreds of rows. Tcl reference
-
regsub -all {\[(\d+)\]} $name {_\1} flatReplaces every match, writing the result into the named variable.
Hint: The last argument is a variable name, and the pattern has a group in it.
The return value is the number of substitutions made, and the new string goes into the variable given last; without that argument the string is returned instead. In the replacement, a backslash and a digit stand for the matching group, so bus bits become underscores here. Tcl reference
-
set nums [regexp -all -inline {-?\d+\.\d+} $line]A list of every match in the line, instead of one or zero.
Hint: Ask what is returned when the match variables are left out.
This is the compact way to pull the numbers out of a report line without a loop. With capture groups, each match contributes the whole match and then each group, so the list has to be read in strides. Tcl reference
-
string trim $line string first ":" $line string range $line 0 $iStrip the whitespace, find a character's position, and take a slice.
Hint: Three small operations that together take a line apart.
Positions are counted from zero and
endmay be used in place of a number, as instring range $line 0 end-1. Between these and the pattern commands, most report parsing needs no regular expression at all. Tcl reference