1

我正在编写一个 TCL 脚本来解析 Firebug 分析控制台的 HTML 输出。首先,我只想累积每个文件的方法调用次数。为此,我正在尝试使用嵌套字典。我似乎得到了第一级正确(其中文件是键,方法是值),但不是第二级,嵌套级别,其中方法是值,计数是键。

我已经阅读了字典的更新命令,所以我愿意使用它进行重构。我的 TCL 使用时断时续,因此在此先感谢您的帮助。下面是我的代码和一些示例输出

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 ""
}

输出:

file: localhost:50267
(?): 0
e: 0

file: Defaults.js
toDictionary: 0
(?): 0
Renderer: 0

file: jquery.cookie.js
cookie: 0
decoded: 0
(?): 0
4

1 回答 1

2

dict set命令与 Tcl 中的任何 setter 一样,将变量作为其第一个参数。我敢打赌:

dict set $file_method_calls $js_method [expr 1 + $file_method_call_counts]

真的应该读:

dict set file_method_calls $js_method [expr {1 + $file_method_call_counts}]

(另外,为速度和安全做好准备。)

于 2013-04-04T15:01:09.177 回答