英文:
Tcl code for compare duplicate names with their values
问题
以下是修改后的代码,用于获取预期的输出:
set inputFile [open $input_def r]
set outputFile [open $output_filename w+]
set lineNumber 0
set skipLines 0
set skipNextLine 0
while {[gets $inputFile line] >= 0} {
incr lineNumber
if {$skipNextLine > 0} {
# Skip current line and reduce skip count
set skipNextLine [expr $skipNextLine - 1]
continue
}
if {($lineNumber % 3) == 1} {
# Extract the first word until the first tab or space
set firstWord [string trimleft [string range $line 0 [expr {[string first "\t" $line] - 1}]]]
# Compare with output file data
set outputData [read $outputFile]
set outputLine [lindex [split $outputData "\t"] 0]
if {[string match "*$firstWord*" $outputLine]} {
# Match found, skip current line and the next two lines
set skipNextLine 2
continue
}
}
# Write the line to the output file
puts $outputFile $line
}
close $inputFile
close $outputFile
此修改代码应该会按预期工作,能够正确地比较行并生成所需的输出。
英文:
i have one file which has 3m of data and the format is given below. i want compare lines inthe file (each lines have there own data). if the same line found compare with its data and store it in output file (small value data), and if unique line found write to output file.
file : sample_data.tcl
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_0__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_60__11_
**0.777900(5.709091%)** 0.799630(3.05152%) 0.021730(2.633939%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_0__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_58__11_
**0.97870(6.076364%)** 0.79990(3.033939%) 0.021800(2.64424%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_1__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_1__44_
**0.778180(5.675152%)** 0.798220(3.246061%) 0.020040(2.429091%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_0__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_58__11_
**0.77870(5.676364%)** 0.79990(3.033939%) 0.021800(2.64424%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_1/i_tx_top/i_csr/i_reg/txdatgen_seed_udp_reg3_io3_nt_q_reg_20_
**0.777320(5.779394%)** 0.803050(2.660606%) 0.025730(3.118788%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_1__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_1__22_
** 0.777720(5.730909%)** 0.797750(3.303030%) 0.020030(2.427879%)
if match found compare with highlighted data
expected output
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_0__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_60__11_
**0.777900(5.709091%)** 0.799630(3.05152%) 0.021730(2.633939%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_1__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_1__44_
**0.778180(5.675152%)** 0.798220(3.246061%) 0.020040(2.429091%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_0__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_58__11_
**0.77870(5.676364%)** 0.79990(3.033939%) 0.021800(2.64424%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_1/i_tx_top/i_csr/i_reg/txdatgen_seed_udp_reg3_io3_nt_q_reg_20_
**0.777320(5.779394%)** 0.803050(2.660606%) 0.025730(3.118788%)
i_top/i_module_wrapper_top_0/i_module_dbe_top_0/i_ctrl_top/u_mcu_system/u_ibex_top/gen_rams_gen_rams_inner_1__gen_noscramble_rams_data_bank/gen_generic_u_impl_generic/mem_reg_1__22_
** 0.777720(5.730909%)** 0.797750(3.303030%) 0.020030(2.427879%)
the code what i wrote to get expected output but i am getting wrong data
i just confuted with this logic now
set inputFile [open $input_def r]
set outputFile [open $output_filename w+]
set lineNumber 0
set skipLines 0
while {[gets $inputFile line] >= 0} {
incr lineNumber
if {($lineNumber % 3) == 1} {
# Extract the first word until the first tab or space
set firstWord [string trimleft [string range $line 0 [expr {[string first "\t" $line] - 1}]]]
# Compare with output file data
set outputData [gets $outputFile]
if {[string match "*$firstWord*" $outputData]} {
# Match found, skip current line and the next two lines
set skipLines 3
continue
}
}
if {$skipLines > 0} {
# Skip current line
set skipLines [expr $skipLines - 1]
continue
}
# Write the line to the output file
puts $outputFile $line
}
close $inputFile
close $outputFile
can anyone give solution for this
答案1
得分: 3
你可以尝试类似以下的代码:
# 读取数据到行列表
set inputFile [open $input_def r]
set lines [split [read $inputFile] \n]
close $inputFile
# 每组3行数据是一个“项目”
# 使用字典存储项目,以第一行作为键
set uniq {}
foreach {a b c} $lines {
dict set uniq $a [join [list $a $b $c] \n]
}
set outputFile [open $output_filename w+]
dict for {key value} $uniq {
puts $outputFile $value
}
close $outputFile
请注意,此代码是Tcl编程语言的示例代码,用于将数据按照每组三行的方式处理,并将唯一的项目保存到字典中,然后将结果写入输出文件。
英文:
You could try something like:
# read the data into lines
set inputFile [open $input_def r]
set lines [split [read $inputFile] \n]
close $inputFile
# each group of 3 lines is an "item"
# store the items in a dictionary, keyed by the first of the three lines
set uniq {}
foreach {a b c} $lines {
dict set uniq $a [join [list $a $b $c] \n]
}
set outputFile [open $output_filename w+]
dict for {key value} $uniq {
puts $outputFile $value
}
close $outputFile
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论