Main Content

Arrays in Tcl - a demonstration

Archive - Originally posted on "The Horse's Mouth" - 2007-11-10 08:14:46 - Graham Ellis

# Array in Tcl
 
set town(Wycombe) 120000
set town(Marlow) 32567
set town(Amersham) 17000
set town(Melksham) 24000
set town(1) xxxxxx
set town(1.) yyyyyyy
 
# Note that you CAN use numbers as subscripts but you
# must be careful because element "1" is different to
# element "1." ... or element "1.0"
 
set place Oxford
set town($place) 140000
 
# Note - need to be careful as to whether to put a $ in
# front of the array name, the subscript, both, or neither
# as all rour options are valid in the right circumstance
 
puts "Please choose a town "
set said [gets stdin]
 
if {[info exists town($said)]} {
puts "$said has a population of $town($said)"
} else {
puts "dunno"
}
 
set keylist [lsort [array names town]]
foreach said $keylist {
puts "$said has a population of $town($said)"
}