Dictionary 写入tcl字典
我对tcl字典比较陌生,没有看到关于如何初始化空字典、循环日志和将数据保存到其中的好文档。最后,我想打印一个如下所示的表:Dictionary 写入tcl字典,dictionary,data-structures,tcl,associative-array,tclsh,Dictionary,Data Structures,Tcl,Associative Array,Tclsh,我对tcl字典比较陌生,没有看到关于如何初始化空字典、循环日志和将数据保存到其中的好文档。最后,我想打印一个如下所示的表: - Table: HEAD1 Step 1 Start Time End Time Step 2 Start Time End Time ** - Log: ** HEAD1 Step1 Start Time : 10am . .
- Table:
HEAD1
Step 1 Start Time End Time
Step 2 Start Time End Time
**
- Log:
**
HEAD1
Step1
Start Time : 10am
.
.
.
End Time: 11am
Step2
Start Time : 11am
.
.
End time : 12pm
HEAD2
Step3
Start Time : 12pm
.
.
.
End Time: 1pm
Step4
Start Time : 1pm
.
.
End time : 2pm
你真的不必在Tcl中初始化一个空字典——你只需开始使用它,它就会随着你的操作而被填充。如前所述,dict手册页是最好的开始方式。 此外,我建议您检查,因为您可以很好地使用它来解析文本文件 由于没有更好的方法来做atm,我拼凑了一个简短的示例代码,应该可以让您开始。使用它作为开始提示,根据您的特定日志布局进行调整,并添加一些防御措施以防止意外输入错误
# The following line is not strictly necessary as Tcl does not
# require you to first create an empty dictionary.
# You can simply start using 'dict set' commands below and the first
# one will create a dictionary for you.
# However, declaring something as a dict does add to code clarity.
set tableDict [dict create]
# Depending on your log sanity, you may want to declare some defaults
# so as to avoid errors in case the log file misses one of the expected
# lines (e.g. 'HEADx' is missing).
set headNumber {UNKNOWN}
set stepNumber {UNKNOWN}
set start {UNKNOWN}
set stop {UNKNOWN}
# Now read the file line by line and extract the interesting info.
# If the file indeed contains all of the required lines and exactly
# formatted as in your example, this should work.
# If there are discrepancies, adjust regex accordingly.
set log [open log.txt]
while {[gets $log line] != -1} {
if {[regexp {HEAD([0-9]+)} $line all value]} {
set headNumber $value
}
if {[regexp {Step([0-9]+)} $line all value]} {
set stepNumber $value
}
if {[regexp {Start Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
}
# NOTE: I am assuming that your example was typed by hand and all
# inconsistencies stem from there. Otherwise, you have to adjust
# the regular expressions as 'End Time' is written with varying
# capitalization and with inconsistent white spaces around ':'
if {[regexp {End Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
# NOTE: This short example relies heavily on the log file
# being formatted exactly as described. Therefore, as soon
# as we find 'End Time' line, we assume that we already have
# everything necessary for the next dictionary entry
dict set tableDict HEAD$headNumber Step$stepNumber StartTime $start
dict set tableDict HEAD$headNumber Step$stepNumber EndTime $stop
}
}
close $log
# You can now get your data from the dictionary and output your table
foreach head [dict keys $tableDict] {
puts $head
foreach step [dict keys [dict get $tableDict $head]] {
set start [dict get $tableDict $head $step StartTime]
set stop [dict get $tableDict $head $step EndTime]
puts "$step $start $stop"
}
}
你真的不必在Tcl中初始化一个空字典——你只需开始使用它,它就会随着你的操作而被填充。如前所述,dict手册页是最好的开始方式。 此外,我建议您检查,因为您可以很好地使用它来解析文本文件 由于没有更好的方法来做atm,我拼凑了一个简短的示例代码,应该可以让您开始。使用它作为开始提示,根据您的特定日志布局进行调整,并添加一些防御措施以防止意外输入错误
# The following line is not strictly necessary as Tcl does not
# require you to first create an empty dictionary.
# You can simply start using 'dict set' commands below and the first
# one will create a dictionary for you.
# However, declaring something as a dict does add to code clarity.
set tableDict [dict create]
# Depending on your log sanity, you may want to declare some defaults
# so as to avoid errors in case the log file misses one of the expected
# lines (e.g. 'HEADx' is missing).
set headNumber {UNKNOWN}
set stepNumber {UNKNOWN}
set start {UNKNOWN}
set stop {UNKNOWN}
# Now read the file line by line and extract the interesting info.
# If the file indeed contains all of the required lines and exactly
# formatted as in your example, this should work.
# If there are discrepancies, adjust regex accordingly.
set log [open log.txt]
while {[gets $log line] != -1} {
if {[regexp {HEAD([0-9]+)} $line all value]} {
set headNumber $value
}
if {[regexp {Step([0-9]+)} $line all value]} {
set stepNumber $value
}
if {[regexp {Start Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
}
# NOTE: I am assuming that your example was typed by hand and all
# inconsistencies stem from there. Otherwise, you have to adjust
# the regular expressions as 'End Time' is written with varying
# capitalization and with inconsistent white spaces around ':'
if {[regexp {End Time : ([0-9]+(?:am|pm))} $line all value]} {
set start $value
# NOTE: This short example relies heavily on the log file
# being formatted exactly as described. Therefore, as soon
# as we find 'End Time' line, we assume that we already have
# everything necessary for the next dictionary entry
dict set tableDict HEAD$headNumber Step$stepNumber StartTime $start
dict set tableDict HEAD$headNumber Step$stepNumber EndTime $stop
}
}
close $log
# You can now get your data from the dictionary and output your table
foreach head [dict keys $tableDict] {
puts $head
foreach step [dict keys [dict get $tableDict $head]] {
set start [dict get $tableDict $head $step StartTime]
set stop [dict get $tableDict $head $step EndTime]
puts "$step $start $stop"
}
}
这些不适合您吗?这些不适合您吗?Tcl会在后台自动初始化(并自动翻译)类型,这就是为什么我们鼓励人们在编程时不要考虑类型。不过,就脚本理解而言,传达您使用特定值的意图肯定是有意义的(关于给定类型)指定范围。因此,虽然不必强制遵守,但作为一份文档,
set tableDict[dict create]
具有一定的价值。非常感谢您提供的详细示例。我不太确定如何使用字典中的变量。这肯定有助于STCL自动初始化(和自动翻译)在幕后为您提供类型,这就是为什么我们鼓励人们在编程时不要考虑类型。然而,就脚本理解而言,传达您对给定范围使用特定值(给定类型)的意图肯定是有意义的。因此,在不被迫遵守的情况下,set tableDict[dict create]
作为一篇文档有一定的价值。非常感谢您提供的详细示例。我不太确定如何在字典中使用我的变量。这肯定会有帮助