Skip to content

Instantly share code, notes, and snippets.

@hiscaler
Created May 31, 2012 06:56
Show Gist options
  • Select an option

  • Save hiscaler/2841559 to your computer and use it in GitHub Desktop.

Select an option

Save hiscaler/2841559 to your computer and use it in GitHub Desktop.
ASP Array Helper
<%
' Array Helper
Class Class_ArrayHelper
'**************************************************
'函数名:isNoneArray
'作 用:检测是空数组
'**************************************************
Public Function isNoneArray(Byval Arr)
If NOT IsArray(Arr) Then
isNoneArray = True
Exit Function
End If
err.clear
on error resume next
If Ubound(arr) < 0 Then
isNoneArray = True
Exit Function
End If
If err.number <> 0 Then
isNoneArray = True
err.clear
Exit Function
End If
err.clear
isNoneArray = false
End Function
'**************************************************
'函数名:isIntegerArray
'作 用:检测是整形数组
'**************************************************
Public Function isIntegerArray(Byval Arr)
Dim i
If NOT IsArray(Arr) Then
isIntegerArray=False
Exit Function
End If
err.clear
on error resume next
If Ubound(arr)<0 Then
isIntegerArray=False
Exit Function
End If
For i=0 to Ubound(arr)
If not isinteger(arr(i)) then
isIntegerArray=False
Exit Function
End If
Next
err.clear
isIntegerArray=True
End Function
'*************************************************
'函数名:indexOfArray
'作 用:查找对象在数组中的索引号
'参 数:arr : 数组
' val : 查找对象
'返回值:对象在数组中的索引号,没有找到则返回-1
'*************************************************
Public Function indexOfArray(arr, val)
on error resume next
Dim retvalue, i, n
retvalue = -1
If NOT isArray(arr) OR UtilHelper.checkNull(val) Then
indexOfArray = retvalue
Exit function
End If
n = Ubound(arr)
For i = Lbound(arr) to n
If CStr(trim(arr(i))) = CStr(val) Then
retvalue = i
Exit For
End If
Next
indexOfArray = retvalue
If err.number <> 0 Then err.clear()
End Function
' 检测指定的数据在数组中是否存在
Public Function inArray(ByVal value, ByVal arr)
on error resume next
Dim isIn, i
isIn = False
If NOT isArray(arr) OR UtilHelper.checkNull(value) Then
inArray = isIn
Exit function
End If
For i = Lbound(arr) To Ubound(arr)
If CStr(Trim(arr(i))) = CStr(value) Then
isIn = True
Exit For
End If
Next
inArray = isIn
If err.number <> 0 Then err.clear()
End Function
' 找出第二个数组相对于第一个数组的差异
Public Function arrayDiff(ByVal arr1, ByVal arr2)
If (isNoneArray(arr1) OR isNoneArray(arr2)) Then
arrayDiff = Array()
Else
Dim i, j, diff
j = 0
ReDim diff(j)
For i = 0 To Ubound(arr2)
If (indexOfArray(arr1, arr2(i)) = -1) Then
ReDim Preserve diff(j)
diff(j) = arr2(i)
j = j + 1
End If
Next
arrayDiff = diff
End If
End Function
' 数组合并
Public Function arrayMerge(ByVal arr1, ByVal arr2)
If ((NOT isArray(arr1)) AND (NOT isArray(arr2)) AND isNoneArray(arr1) AND isNoneArray(arr2)) Then
arrayMerge = Array()
Else
Dim i, count, result
ReDim result(Ubound(arr1) + Ubound(arr2) + 1)
For i = 0 To Ubound(arr1)
result(i) = arr1(i)
Next
count = Ubound(arr1)
For i = 0 To Ubound(arr2)
If (indexOfArray(arr1, arr2(i)) = -1) Then
result(count + i + 1) = arr2(i)
End If
Next
arrayMerge = result
End If
End Function
' 保持数组中的数据唯一性
Public Function arrayUnique(ByVal arr)
If (IsArray(arr)) Then
If (isNoneArray(arr)) Then
arrayUnique = Array()
Else
Dim i, newArr()
Dim j:j = 0
ReDim newArr(j)
For i = 0 To Ubound(arr)
If (indexOfArray(newArr, arr(i)) = -1) Then
ReDim Preserve newArr(j)
newArr(j) = arr(i)
j = j + 1
End If
Next
arrayUnique = newArr
End If
Else
arrayUnique = Array()
End If
End Function
' 移除数组中的空元素
Public Function removeArrayEmpty(ByVal arr)
If (IsArray(arr)) Then
If (isNoneArray(arr)) Then
removeArrayEmpty = arr
Else
Dim i, c, newArr()
Dim j:j = 0
ReDim newArr(j)
For i = 0 To Ubound(arr)
If (NOT UtilHelper.checkNull(arr(i))) Then
ReDim Preserve newArr(j)
newArr(j) = arr(i)
j = j + 1
End If
Next
removeArrayEmpty = newArr
End If
Else
removeArrayEmpty = arr
End If
End Function
Public Function arrayValues(ByVal arr)
arrayValues = Split(Join(arr, "{{}}"), "{{}}")
End Function
' push a value to arr end
Public Function push(ByVal arr, ByVal value)
Dim i:i = Ubound(arr) + 1
ReDim Preserve arr(i)
arr(i) = value
push = arr
End Function
Public Function isset(ByVal arr, ByVal key)
Dim res:res = False
If (NOT isNoneArray(arr)) Then
Dim max:max = Ubound(arr)
If (key <= max) Then
res = True
End If
End If
isset = res
End Function
Public Function unset(ByVal arr, ByVal value)
If (NOT isNoneArray(arr)) Then
Dim index:index = indexOfArray(arr, value)
If (index <> -1) Then
Dim aSize:aSize = Ubound(arr)
Dim i, j:j = 0
Dim res
ReDim res(j)
For i = 0 To aSize
If (arr(i) <> value) Then
ReDim Preserve res(j)
res(j) = arr(i)
j = j + 1
End If
Next
unset = res
Else
unset = arr
End If
Else
unset = arr
End If
End Function
End Class
Dim ArrayHelper
set ArrayHelper = new Class_ArrayHelper
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment