简体   繁体   English

VBScript循环建议

[英]VBScript loop advice

sqlRows = rst.GetRows()
i = 0

For Each element In sqlRows
    If i > 0 And i < sizeOfState + 1 Then 
        SmartTags("visu_state_on")(i - 1) = element
    ElseIf i > sizeOfState And i < 2 * sizeOfState + 1 Then
        SmartTags("visu_state_off")(i - sizeOfState - 1) = element
    ElseIf i > (2 * sizeOfState ) And i < 2 * sizeOfState + sizeOfMeasurement + 1 Then
        SmartTags("visu_limits_right")(i - (2 * sizeOfState - 1)) = element
    ElseIf i > 2 * sizeOfState + sizeOfMeasurement And i < 2 * (sizeOfStanja + sizeOfMeasurement ) + 1 Then
        SmartTags("visu_limits_left")(i - (2 * sizeOfState + sizeOfMeasurement )) = element
    End If

    i = i + 1
Next

With code above I'm looping through array sqlRows and with variable i I'm filling other four arrays with data from sqlRows . 使用上面的代码,我遍历数组sqlRows并使用变量i用来自sqlRows数据填充其他四个数组。

This solution works but I'm wondering is there more elegant way to achieve the same. 此解决方案有效,但我想知道是否还有更优雅的方法可以实现相同目的。

  • sqlRows is array with dimensions 343x1, sqlRows是尺寸为343x1的数组,
  • visu_state_on is array with dimensions 8x1, visu_state_on是尺寸为8x1的数组,
  • visu_state_off is array with dimensions 8x1, visu_state_off是尺寸为8x1的数组,
  • visu_limits_right is array with dimensions 160x1, visu_limits_right是尺寸为160x1的数组,
  • visu_limits_left is array with dimensions 160x1, visu_limits_left是尺寸为160x1的数组,

and variables sizeOfState and sizeOfMeasurement are just there that I can calculate indexes for these four arrays. 变量sizeOfStatesizeOfMeasurement就在那里,我可以为这四个数组计算索引。

For one thing you can remove the first clause from each of your conditions, because they will always be true 一方面,您可以从每个条件中删除第一个子句,因为它们始终为真

  • i starts with the value 0 and is always incremented, so the value will never be less than zero. i从0开始,并且总是递增,所以该值永远不会小于零。
  • If a value is not less than n + 1 (condition in previous ElseIf ) then it's guaranteed to be greater than n . 如果值不小于n + 1 (以前的ElseIf条件),则可以保证它大于n

Also, VBScript does have a <= comparison operator, so it's better to compare i <= n rather than i < n + 1 . 此外,VBScript确实具有<=比较运算符,因此比较i <= n而不是i < n + 1更好。

I would also recommend calculating values that won't change inside the loop just once outside the loop. 我还建议计算一次在循环外不会改变的值。 Re-calculating them with each loop cycle is a waste of resources. 在每个循环周期中重新计算它们是浪费资源。

Adding an Else branch to handle values greater than 2 * (sizeOfStanja + sizeOfMeasurement) might be a good idea too. 添加Else分支来处理大于2 * (sizeOfStanja + sizeOfMeasurement)也是一个好主意。

sqlRows = rst.GetRows()
i = 0

ref1 = 2 * sizeOfState
ref2 = ref1 + sizeOfMeasurement
ref3 = 2 * (sizeOfStanja + sizeOfMeasurement)

For Each element In sqlRows
  If i <= sizeOfState Then 
    SmartTags("visu_state_on")(i - 1) = element
  ElseIf i <= ref1 Then
    SmartTags("visu_state_off")(i - sizeOfState - 1) = element
  ElseIf i <= ref2 Then
    SmartTags("visu_limits_right")(i - ref1 + 1) = element
  ElseIf i <= ref3 Then
    SmartTags("visu_limits_left")(i - ref2) = element
  Else
    'handle i > ref3 here
  End If

  i = i + 1
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM