Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trace variable : trying to get value of array element

Status
Not open for further replies.

jjfjr

Programmer
Mar 10, 2004
13
US
Hi;

I am trying to trace a variable which is an array element. The element holds a hexadecimal number which can be changed. If the user changes it, I need to make sure that a valid hex number was put in. Instead of getting the value of the element, I am getting the name of the element. Pieces of my trace and proc code are as follows:

trace variable sic${numrad} w eval_sic


proc eval_sic {sic_check index op}{
.
.
.
set choice {tk_messageBox -type ok -message $sic_check}
.
.
set str_len [string length $sic_check]
set choice {tk_messageBox -type ok -message $str_len}
if { $str_len <= 2 && $sic_check != ""} {
if {![$sic_check is xdigit]} {

display_error .....
.
2 hex digits are entered and I need to grab them as a string and eval both (i.e. look at FF not evaluate F then F}
the messagebox lines are for purposes of seeing the values. $sic_check comes out as SIC6 (the name of the array element}
and str_len evaluates to 4 (the number of chars in the element name) What I want is to grab the value that is typed into the element and its properties.

Any help is greatly appreciated;

jjfjr
 
I'm not sure to understand your need.
If you want to check if an element of an array is well-formed, here is an example:
Code:
  # check if all element of an array is a well formed hexa number
  trace add variable myarray write check:hexa
  set hexdigits {0 1 2 3 4 5 6 7 8 9 0 a b c d e f A B C D E F}
  proc check:hexa {var index op}   {
    upvar $var myarray
    set value $myarray($index)
    foreach digit [split $value ""]     {
      if {[lsearch -exact $::hexdigits $digit] == -1}       {
        error "${var}($index) value is ill-formed: \"$value\"" 
      }
    }
  }
  set myarray(0) 0f
  catch { set myarray(1) ill-formed } msg
  puts $msg
Here the trick is to use upvar in the trace callback to get access to the array.
This is explained in the description of the trace command man page:

Command executes in the same context as the code that invoked the traced operation: if the variable was accessed as part of a Tcl procedure, then command will have access to the same local variables as code in the procedure. This context may be different than the context in which the trace was created. If command invokes a procedure (which it normally does) then the procedure will have to use upvar or uplevel if it wishes to access the traced variable. Note also that name1 may not necessarily be the same as the name used to set the trace on the variable; differences can occur if the access is made through a variable defined with the upvar command.


More info at:
(If you use an older version of Tcl, the syntax of the trace is slightly different)

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top