本帖最后由 yu2n 于 2015-8-8 14:03 编辑
计划使用一个固定长度的数组循环复写来记录日志,写了一个ArrayShift函数,此函数并无问题,但是此函数使用了两个数组,如何缩减为一个?求优化…- 'ArrayShift.vbs
- Dim arrStr, sInfo
- arrStr = Split("0,1,2,3,4,5,6,7,8,9", ",")
- sInfo = "原字符串:" & Join(arrStr, ",") & vbCrLf
- sInfo = sInfo & "偏移+2:" & Join(ArrayShift(arrStr, 2), ",") & vbCrLf
- sInfo = sInfo & "偏移-2:" & Join(ArrayShift(arrStr, -2), ",") & vbCrLf
- sInfo = sInfo & "偏移+10086:" & Join(ArrayShift(arrStr, 10086), ",") & vbCrLf
- sInfo = sInfo & "偏移-10086:" & Join(ArrayShift(arrStr, -10086), ",") & vbCrLf
- MsgBox sInfo
-
- Function ArrayShift(ByVal arrStr, ByVal nShift)
- Dim arr, nLength, i, s1, s, nOffSet
- arr = arrStr
- nLength = UBound(arrStr)
- For i = 0 To nLength Step 1
- nOffSet = ((i + nShift) Mod (nLength + 1))
- If nOffSet >= 0 Then
- arr(i) = arrStr(nOffSet)
- ElseIf nOffSet < 0 Then
- nOffSet = (nLength + 1) + nOffSet
- arr(i) = arrStr(nOffSet)
- End If
- Next
- ArrayShift = arr
- End Function
复制代码 示例结果如下:- 原字符串:0,1,2,3,4,5,6,7,8,9
- 偏移+2:2,3,4,5,6,7,8,9,0,1
- 偏移-2:8,9,0,1,2,3,4,5,6,7
- 偏移+10086:6,7,8,9,0,1,2,3,4,5
- 偏移-10086:4,5,6,7,8,9,0,1,2,3
复制代码
|