Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-07-06
17:05 • Ticket [57e44de76e] Array Read Trace fires only if Array has been written to... artifact: 312bff4c87 user: ferrieux
2015-07-05
23:33 • Ticket [57e44de76e]: 3 changes artifact: 256220657c user: venkat
21:49 • Ticket [57e44de76e]: 3 changes artifact: b636a40753 user: venkat
20:07 • New ticket [57e44de76e]. artifact: 31b5928c23 user: venkat

Array Read Trace fires only if Array has been written to

Open57e44de76e71ba13b896571404da2451fcc6c4f1Created 11 years agoUpdated 11 years ago
Descriptionplain2015-07-05 20:07:16
=== start of a.tcl ===

proc ::stracer {junk var op} {
    if {$var == ""} return
    switch $op {
        r {
            puts "Read Fired"
            set ::a($var) 5
        }
        w {
            puts "Write Fired"
        }
    }
}

trace variable ::a rw ::stracer

# set a(test2) 5
puts "$a(test1)"

=== end of a.tcl ===

Running:

% tclsh8.6 a.tcl
can't read "a(test1)": no such variable
    while executing
"puts "$a(test1)""
    (file "a.tcl" line 17)


Now remove the commented line (Remove # in "# set a(test2) 5)

% tclsh8.6 a.tcl
Write Fired
Read Fired
5

----

Why does the write to the array change the behaviour of the read trace?
venkat Ticket [57e44de76e]: 3 changes [History diff]2015-07-05 21:49:07
venkatplain2015-07-05 23:33:15
Instead of the write : "set a(test2) 5",  even a "array set a [list]" makes it work.     This might be a documentation issue rather than a code issue.
ferrieuxplain2015-07-06 17:05:02
Yes: most likely the var must gain "array status" before the trace is set.
Log in (anonymous is fine) to add a comment.