Friday 7 January 2011

How to iterate through the topmost indices only in a multidimensional tcl array

Using ,(comma)s in tcl array indices is the standard way of using them as multidimensional arrays.

# Test:
if {[info exists aTestHostInfo]} {puts exists}
set aTestHostInfo(node1,ip) 192.168.18.82
set aTestHostInfo(node1,swversion) "Wed Jan 5 21:27:33 GMTST 2011,FORMAL,18666,,,"

set aTestHostInfo(node2,ip) 192.168.18.83
set aTestHostInfo(node2,swversion) "Wed Jan 5 21:27:33 GMTST 2011,FORMAL,18666,,,"

set aTestHostInfo(node3,ip) 192.168.18.84

set aTestHostInfo(node4,foop) 192.168.18.85

# This seems to be a little bit of info missing from the internet.
# How to iterate through the topmost indices only in a multidimensional tcl array:
  if {[info exists aTestHostInfo]} {
    # go through topmost index of multidim array
    foreach sCard [lsort -uniq [regsub -all {,\w*} [array names aTestHostInfo] "" ]] {
      if {![info exists aTestHostInfo($sCard,ip)]} {set aTestHostInfo($sCard,ip) UNKNOWN}
      if {![info exists aTestHostInfo($sCard,swversion)]} {set aTestHostInfo($sCard,swversion) UNKNOWN}
      set swdlOutput "$sCard,$aTestHostInfo($sCard,ip),$aTestHostInfo($sCard,swversion)"
      puts $swdlOutput
    }
  }





No comments: