TCL 脚本需要帮助来优化 If 并更正第三个输出
TCL script Need Help to Optimise If and also Correction of the 3rd Output
我有以下代码,想知道如何:
1) 优化"if",这样以后如果我有10个新的customer_id,我就不用再写10次if-then-else语句了,有什么建议吗?
2) 注意 3 号的输出是错误的,它不应该计算(除以 1.5)我该如何更正它?
TCL 源脚本
set VG01 0
set VT01 0
set VG02 0
set VT02 0
set VG03 0
set VT03 0
set VGtable01 150
set VTtable01 15
set VGtable02 250
set VTtable02 25
set VGtable03 350
set VTtable03 35
set sCompareCustomerID01 "0001"
set sCompareCustomerID02 "0002"
set sCompareCustomerID03 "0003"
set sCustomer01 "0001"
set sCustomer02 "0002"
set sCustomer03 "0004"
if {[string compare $sCompareCustomerID01 $sCustomer01 ] == 0 ||
[string compare $sCompareCustomerID02 $sCustomer02 ] == 0 ||
[string compare $sCompareCustomerID03 $sCustomer03 ] == 0 } {
set VG01 [expr $VGtable01 / 1.5]
set VT01 [expr $VTtable01 / 1.5]
set VG02 [expr $VGtable02 / 1.5]
set VT02 [expr $VTtable02 / 1.5]
set VG03 [expr $VGtable03 / 1.5]
set VT03 [expr $VTtable03 / 1.5]
} else {
set VG01 $VGtable01
set VT01 $VTtable01
set VG02 $VGtable02
set VT02 $VTtable02
set VG03 $VGtable03
set VT03 $VTtable03
}
puts "Target Value_Group 01: $VG01"
puts "Target Value_Transaction 01: $VT01"
puts "Target Value_Group 02: $VG02"
puts "Target Value_Transaction 02: $VT02"
puts "Target Value_Group 03 : $VG03"
puts "Target Value_Transaction 03: $VT03"
============
输出:
Import started at 26 Mar 2020 04:09:58.
Loading Script DLL
C:\Program Files (x86)\Common Files\Cubeware\cwscript85.dll
MMM-2020
200325_test division 1.5: Starting jobs at 26 Mar 2020 04:09:58
Execute script: 200325_test division 1.5 - Started at 26 Mar 2020 04:09:58
Target Value_Group 01: 100.0
Target Value_Transaction 01: 10.0
Target Value_Group 02: 166.6666666666667
Target Value_Transaction 02: 16.66666666666667
Target Value_Group 03 : 233.3333333333333 < ------ this should be 350, no calculation require
Target Value_Transaction 03: 23.33333333333333 < ------ this should be 35, no calculation require
Freeing resources
Import completed
Import finished at 26 Mar 2020 04:09:58
好吧,问题是您只有一个 if
,看起来您真的应该为每个客户单独准备一个 if
。要单独解决这个问题,它必须是:
if {[string compare $sCompareCustomerID01 $sCustomer01 ] == 0} {
set VG01 [expr $VGtable01 / 1.5]
set VT01 [expr $VTtable01 / 1.5]
} else {
set VG01 $VGtable01
set VT01 $VTtable01
}
if {[string compare $sCompareCustomerID02 $sCustomer02 ] == 0} {
set VG02 [expr $VGtable02 / 1.5]
set VT02 [expr $VTtable02 / 1.5]
} else {
set VG02 $VGtable02
set VT02 $VTtable02
}
if {[string compare $sCompareCustomerID03 $sCustomer03 ] == 0} {
set VG03 [expr $VGtable03 / 1.5]
set VT03 [expr $VTtable03 / 1.5]
} else {
set VG03 $VGtable03
set VT03 $VTtable03
}
但这并不是真正可扩展的。如果您有更多客户,我建议使用数组而不是普通变量,然后可以将循环与单个 if
:
一起使用
array set VG {
01 0
02 0
03 0
}
array set VT {
01 0
02 0
03 0
}
array set VGtable {
01 150
02 250
03 350
}
array set VTtable {
01 15
02 25
03 35
}
array set sCompareCustomerID {
01 0001
02 0002
03 0003
}
array set sCustomer {
01 0001
02 0002
03 0004
}
foreach customer [array names sCompareCustomerID] {
if {
[info exists sCustomer($customer)] &&
[string compare $sCompareCustomerID($customer) $sCustomer($customer)] == 0
} {
set VG($customer) [expr {$VGtable($customer) / 1.5}]
set VT($customer) [expr {$VTtable($customer) / 1.5}]
} else {
set VG($customer) $VGtable($customer)
set VT($customer) $VTtable($customer)
}
}
puts "Target Value_Group 01: $VG(01)"
puts "Target Value_Transaction 01: $VT(01)"
puts "Target Value_Group 02: $VG(02)"
puts "Target Value_Transaction 02: $VT(02)"
puts "Target Value_Group 03 : $VG(03)"
puts "Target Value_Transaction 03: $VT(03)"
我有以下代码,想知道如何:
1) 优化"if",这样以后如果我有10个新的customer_id,我就不用再写10次if-then-else语句了,有什么建议吗?
2) 注意 3 号的输出是错误的,它不应该计算(除以 1.5)我该如何更正它?
TCL 源脚本
set VG01 0
set VT01 0
set VG02 0
set VT02 0
set VG03 0
set VT03 0
set VGtable01 150
set VTtable01 15
set VGtable02 250
set VTtable02 25
set VGtable03 350
set VTtable03 35
set sCompareCustomerID01 "0001"
set sCompareCustomerID02 "0002"
set sCompareCustomerID03 "0003"
set sCustomer01 "0001"
set sCustomer02 "0002"
set sCustomer03 "0004"
if {[string compare $sCompareCustomerID01 $sCustomer01 ] == 0 ||
[string compare $sCompareCustomerID02 $sCustomer02 ] == 0 ||
[string compare $sCompareCustomerID03 $sCustomer03 ] == 0 } {
set VG01 [expr $VGtable01 / 1.5]
set VT01 [expr $VTtable01 / 1.5]
set VG02 [expr $VGtable02 / 1.5]
set VT02 [expr $VTtable02 / 1.5]
set VG03 [expr $VGtable03 / 1.5]
set VT03 [expr $VTtable03 / 1.5]
} else {
set VG01 $VGtable01
set VT01 $VTtable01
set VG02 $VGtable02
set VT02 $VTtable02
set VG03 $VGtable03
set VT03 $VTtable03
}
puts "Target Value_Group 01: $VG01"
puts "Target Value_Transaction 01: $VT01"
puts "Target Value_Group 02: $VG02"
puts "Target Value_Transaction 02: $VT02"
puts "Target Value_Group 03 : $VG03"
puts "Target Value_Transaction 03: $VT03"
============
输出:
Import started at 26 Mar 2020 04:09:58.
Loading Script DLL
C:\Program Files (x86)\Common Files\Cubeware\cwscript85.dll
MMM-2020
200325_test division 1.5: Starting jobs at 26 Mar 2020 04:09:58
Execute script: 200325_test division 1.5 - Started at 26 Mar 2020 04:09:58Target Value_Group 01: 100.0 Target Value_Transaction 01: 10.0 Target Value_Group 02: 166.6666666666667 Target Value_Transaction 02: 16.66666666666667 Target Value_Group 03 : 233.3333333333333 < ------ this should be 350, no calculation require Target Value_Transaction 03: 23.33333333333333 < ------ this should be 35, no calculation require
Freeing resources
Import completed
Import finished at 26 Mar 2020 04:09:58
好吧,问题是您只有一个 if
,看起来您真的应该为每个客户单独准备一个 if
。要单独解决这个问题,它必须是:
if {[string compare $sCompareCustomerID01 $sCustomer01 ] == 0} {
set VG01 [expr $VGtable01 / 1.5]
set VT01 [expr $VTtable01 / 1.5]
} else {
set VG01 $VGtable01
set VT01 $VTtable01
}
if {[string compare $sCompareCustomerID02 $sCustomer02 ] == 0} {
set VG02 [expr $VGtable02 / 1.5]
set VT02 [expr $VTtable02 / 1.5]
} else {
set VG02 $VGtable02
set VT02 $VTtable02
}
if {[string compare $sCompareCustomerID03 $sCustomer03 ] == 0} {
set VG03 [expr $VGtable03 / 1.5]
set VT03 [expr $VTtable03 / 1.5]
} else {
set VG03 $VGtable03
set VT03 $VTtable03
}
但这并不是真正可扩展的。如果您有更多客户,我建议使用数组而不是普通变量,然后可以将循环与单个 if
:
array set VG {
01 0
02 0
03 0
}
array set VT {
01 0
02 0
03 0
}
array set VGtable {
01 150
02 250
03 350
}
array set VTtable {
01 15
02 25
03 35
}
array set sCompareCustomerID {
01 0001
02 0002
03 0003
}
array set sCustomer {
01 0001
02 0002
03 0004
}
foreach customer [array names sCompareCustomerID] {
if {
[info exists sCustomer($customer)] &&
[string compare $sCompareCustomerID($customer) $sCustomer($customer)] == 0
} {
set VG($customer) [expr {$VGtable($customer) / 1.5}]
set VT($customer) [expr {$VTtable($customer) / 1.5}]
} else {
set VG($customer) $VGtable($customer)
set VT($customer) $VTtable($customer)
}
}
puts "Target Value_Group 01: $VG(01)"
puts "Target Value_Transaction 01: $VT(01)"
puts "Target Value_Group 02: $VG(02)"
puts "Target Value_Transaction 02: $VT(02)"
puts "Target Value_Group 03 : $VG(03)"
puts "Target Value_Transaction 03: $VT(03)"