Tcl array unset tips

This page is part of the Tcl/Tk page of the Airplug documentation.
Back to the Tcl/Tk page / Back to Airplug documentation page.

The following code creates an array and displays its elements.

set tableau(1) un
set tableau(2) deux
set tableau(3) trois
for {set i 1} { $i <= 3 } {incr i} {
  puts stdout "tableau($i) = $tableau($i)"
}

The following instructions delete either a single element or the whole array:

unset tableau(2)
array unset tableau

However, this may fail. Indeed, if the elements of the array are displayed in the GUI, then the unset will not be done. Here is an example:

#!/usr/bin/wish
 
set tableau(1) un
set tableau(2) deux
set tableau(3) trois
 
label .tab2 -textvariable tableau(2)
pack .tab2
 
for {set i 1} { $i <= 3 } {incr i} {
  puts stdout "tableau($i) = $tableau($i)"
}

# destroy .tab2
unset tableau(2)
 
for {set i 1} { $i <= 3 } {incr i} {
  if { [info exists tableau($i) ] } {
    puts stdout "tableau($i) = $tableau($i)"
  }
}

The execution gives:

tableau(1) = un
tableau(2) = deux
tableau(3) = trois
tableau(1) = un
tableau(2) = deux
tableau(3) = trois

Now if the widget tab2 is destroyed before the unset command (by removing the comment # before the destroy command in the previous code), then it works as expected. The execution gives then:

tableau(1) = un
tableau(2) = deux
tableau(3) = trois
tableau(1) = un
tableau(3) = trois

This should be taken into account when arrays are displayed and then modified or reset: destroying the widgets, unset the arrays, resetting the widget.

<br>


This page is part of the Tcl/Tk page of the Airplug documentation.
Back to the Tcl/Tk page / Back to Airplug documentation page.

 
Back to top
en/doc/pub/tcltk/tips/unsetarray.txt · Last modified: 2017/04/04 19:47 (external edit)
 
 
Driven by DokuWiki