Lesson 3 of 78 minBeginner

Lists, the TCL Workhorse

Building and walking lists of objects

Lists are everywhere in TCL and in EDA scripting, because a design is full of lists: lists of cells, ports, clocks, files. Knowing list commands well is most of practical TCL.

Creating and indexing

tcl
set corners {ss ff tt}          ;# a 3-element list
puts [lindex $corners 0]        ;# first element  ->  ss
puts [llength $corners]         ;# how many       ->  3
lappend corners sf              ;# add an element  ->  ss ff tt sf
A list is ordered, zero-indexed elements; lappend adds to the end (click to enlarge)

Looping over a list

foreach is the command you will use most. It runs a body once per element.

tcl
foreach corner $corners {
  puts "Reading library for corner: $corner"
}

# foreach can walk several lists at once
foreach port {clk rst d} dir {input input input} {
  puts "$port is an $dir"
}

Associative arrays

When you want to look something up by a key rather than a position, use an array. An array maps keys to values, like a dictionary.

tcl
set period(clk)  1.25
set period(refclk) 20.0
puts $period(clk)              ;# 1.25
foreach name [array names period] {
  puts "$name -> $period($name)"
}
Pro tip

in real flows you keep settings in arrays or lists at the top of the script (corner names, clock periods, file lists) and loop over them, so adding a corner or a file is a one-line change. Data at the top, logic below, is the mark of a clean EDA script.

Watch out

a TCL list is space-separated, so an element containing spaces must be braced or quoted, or it will be split into several elements. When in doubt build lists with the list command, which quotes correctly for you.

Finished reading?

Mark it complete to track your progress through the path.

Get new lessons by email

New lessons, interview prep and real EDA tool workflows. No spam.

PrevNext