Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Last active May 4, 2021 16:42
Show Gist options
  • Save stephenmm/4d351862ec1525ac33c789cff13a8c3f to your computer and use it in GitHub Desktop.
Save stephenmm/4d351862ec1525ac33c789cff13a8c3f to your computer and use it in GitHub Desktop.
Basic list and dict manipulations
# Basic 101 list commands for .tcl (cannot do $X[$index] has to be [lindex $X $index])
set X [list] ;# NOTE! Good practce to start with empty list (.tcl is calling [] function "list" and returning an empty list)
lappend X a b c ; puts $X ;# a b c
#lappend X {d e f} ; puts $X ;# a b c {d e f} ;# NOTE! This is probably not what you wanted! Creates list of list
lappend X {*}{d e f} ; puts $X ;# a b c d e f ;# NOTE! The {*} tells tcl to expand the list before appending
set X [lreplace $X 1 1 B] ; puts $X ;# a B c d e f ;# NOTE! lreplace does not modify the origianl list so must assign it back
set X [lreplace $X 2 3 C D] ; puts $X ;# a B C D e f
set X [lreplace $X 2 2] ; puts $X ;# a B D e f ;# Delete list element by replacing it with nothing
set X [lreplace $X 3 4] ; puts $X ;# a B D
set idx [lsearch $X "B"] ;# Search for index by value
set X [lreplace $X $idx $idx] ; puts $X ;# a D ;# delete element by index value
set X2 {g h i}
set X [concat $X $X2] ; puts $X ;# a D g h i ;# Another way to add/append elements to existing list
lappend X {j k} ; puts $X ;# a D g h i {j k};# Create nested list of lists ( not concatting the lists )
for {set i 0} { $i < [llength $X]} {incr i} { ;# iterate through a list
puts " X\[$i]=[lindex $X $i]" ;# NOTE! MUST use lindex to access elements in a list...
}
X[0]=a
X[1]=D
X[2]=g
X[3]=h
X[4]=i
X[5]=j k
puts [dict get $X "a"] ;# puts D ;# NOTE! If a list has even number of elements it can be treated as a dict key1 val1 key2 val2 ...
puts [dict get $X "i"] ;# puts {j k}
# Siimple examples of working with dict of dicts
set dct {a b c {d e f {g h} i {j k} } l m} ;# a b c {d e f {g h} i {j k} } l m
puts [dict get $dct "c" "f" "g"] ;# h
puts [dict get $dct "c" "i"] ;# j k
puts [dict get $dct "c" "i" "j"] ;# k
puts [dict get $dct "l"] ;# m
# You can set the inner nested dict value directly:
dict set dct c i j 4 ;# a b c {d e f {g h} i {j 4}} l m
# You can set an internal key using "dict with" and a provided function
dict with dct { set a val1 } ;# a val1 c {d e f {g h} i {j 4}} l m
# Update is similar but it creates a temporary variable tmpVal1 that you can use in a provided function
dict update dct a tmpVal1 { set tmpVal1 BBB } ;# a BBB c {d e f {g h} i {j 4}} l m
# Makes it a little easier to do things to the value and have it applied to the original dict:
dict update dct a bnewval { lappend bnewval bbb } ;# a {BBB bbb} c {d e f {g h} i {j 4}} l m
# You can nest the "dict with" to access deeper inside nested dicts
dict with dct {
# a c and l are all now in local scope and can be modified directly
set a v0_0
dict with c {
# d f and i are now locally scoped and can be directly modified
set d E
lappend f HH hh ;# We actually added another dict here!
}
} ;puts $dct ;# a v0_0 c {d E f {g h HH hh} i {j 4}} l m
# See the new dict of dict we added/appended
puts [dict get $dct "c" "f" "HH"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment