Reputation: 12561
I'm writing a TCL script to parse the HTML output of the Firebug profiling console. To begin with I just want to accumulate the number of method calls on a per-file basis. To do this I'm trying to use nested dictionaries. I seem to have gotten the first level correct (where file is the key and method is the value) but not the second, nested level, where method is the value and count is the key.
I have read about dictionary's update command so I'm open to refactoring using this. My TCL usage is on-again off-again so thanks in advance for any assistance. Below is my code and some sample output
foreach row $table_rows {
regexp {<a class="objectLink objectLink-profile a11yFocus ">(.+?)</a>.+?class=" ">(.+?)\(line\s(\d+)} $row -> js_method js_file file_line
if {![dict exists $method_calls $js_file]} {
dict set method_calls $js_file [dict create]
}
set file_method_calls [dict get $method_calls $js_file]
if {![dict exists $file_method_calls $js_method]} {
dict set file_method_calls $js_method 0
dict set method_calls $js_file $file_method_calls
}
set file_method_call_counts [dict get $file_method_calls $js_method]
dict set $file_method_calls $js_method [expr 1 + $file_method_call_counts]
dict set method_calls $js_file $file_method_calls
}
dict for {js_file file_method_calls} $method_calls {
puts "file: $js_file"
dict for {method_name call_count} $file_method_calls {
puts "$method_name: $call_count"
}
puts ""
}
OUTPUT:
file: localhost:50267
(?): 0
e: 0
file: Defaults.js
toDictionary: 0
(?): 0
Renderer: 0
file: jquery.cookie.js
cookie: 0
decoded: 0
(?): 0
Upvotes: 1
Views: 80
Reputation: 137717
The dict set
command, like any setter in Tcl, takes the name of a variable as its first argument. I bet that:
dict set $file_method_calls $js_method [expr 1 + $file_method_call_counts]
should really read:
dict set file_method_calls $js_method [expr {1 + $file_method_call_counts}]
(Also, brace your expressions for speed and safety.)
Upvotes: 2