简单的ASP数组类

2012-08-29  金城  2168

简单的ASP数组类

可以添加、删除、替换、清空、转换为字符串列表等操作

使用简单数组的好处就是能在内存中解决问题,不象数据库那样可能进行较多的磁盘检索。

<%
'' 数组类
class CLS_ArrayList
   dim Arr,SP
   private sub class_initialize
       Arr=array()
       SP=chr(12)
   end sub
   private sub class_terminate
       Erase Arr
   end sub

   public function Exists(v) '' 是否存在值
       if ubound(Arr)>-1 then
           for each a in Arr
               if a=v then
                   Exists=true
                   exit function
               end if
           next
       else
           Exists=false
       end if
   end function

   public function Add(v) '' 添加值
       dim u
       u=ubound(Arr)+1
       redim preserve Arr(u)
       Arr(u)=v
       Add=Arr
   end function

   public function AddKey(v) ''添加键
       if not Exists(v) then Add(v)
   end function
   
   function Delete(v) '' 删除值
       dim i,j,u
       u=ubound(Arr)
       for i=0 to u
           if v=Arr(i) then exit for
       next
       if i<u then
           for j=i to u-1
               Arr(j)=Arr(j+1)
           next
           redim preserve Arr(u-1)
           Delete v
       elseif i=u then
           if Arr(u)=v then
               redim preserve Arr(u-1)   
               Delete v
           end if
       end if
   end function
   
   public sub Modify(v,p) ''替换值
       dim i
       for i=0 to ubound(Arr)
           if Arr(i)=v then Arr(i)=p
       next
   end sub
   
   public function ToList() '' 输出逗号隔开的字符串
       dim rlt
       for each a in Arr
           rlt=rlt &SP &a
       next
       if rlt<>empty then
           if left(rlt,1)=SP then rlt=right(rlt,len(rlt)-1)
       end if
       ToList=replace(rlt,SP,",")
   end function
   
   public function ToListEx(separator) '' js's join 输出指定分割符的字符串
       dim rlt
       for each a in Arr
           rlt=rlt &SP &a
       next
       if rlt<>empty then
           if left(rlt,1)=SP then rlt=right(rlt,len(rlt)-1)
       end if
       ToListEx=replace(rlt,SP,separator)
       '' 方法2
       '' ToListEx=join(Arr,separator)
   end function
   
   sub Clear() '' 清空数组
       redim arr(-1)
   end sub
   
   sub ClearEmpty '' 清除空值
       dim i,j,u
       u=ubound(Arr)
       for i=0 to u
           if typename(Arr(i))="Empty" then
               exit for
           elseif typename(Arr(i))="Null" then
               exit for
           elseif typename(Arr(i))="String" then
               if Arr(i)="" then exit for
           end if
       next
       if i<u then
           for j=i to u-1
               Arr(j)=Arr(j+1)
           next
           redim preserve Arr(u-1)
           ClearEmpty
       elseif i=u then
           if typename(Arr(u))="Empty" then
               redim preserve Arr(u-1)   
               ClearEmpty
           elseif typename(Arr(u))="Null" then
               redim preserve Arr(u-1)   
               ClearEmpty
           elseif typename(Arr(u))="String" then
               if Arr(u)="" then
                   redim preserve Arr(u-1)   
                   ClearEmpty
               end if
           end if
       end if
   end sub
   
   sub KeepInt '' 保留整数值
       dim i,lst
       for each a in arr
           if isNumeric(a) then
               lst=lst &SP &   fix(cdbl(a))
           end if
       next
       if lst=empty then
           Arr=Array()
       else
           if left(lst,1)=SP then lst=right(lst,len(lst)-1)
           Arr=split(lst,SP)
       end if
   end sub
   
   sub KeepStr ''保留字符串
       dim i,lst
       for each a in arr
           if typename(a)="String" then
               if a<>"" then lst=lst &SP &   a
           elseif typename(a)="Date" then
               lst=lst &SP &   a
           elseif typename(a)="Empty" then
           elseif typename(a)="Boolean" then
           elseif typename(a)="Null" then
           elseif isNumeric(a) then
               lst=lst &SP &   a
           end if
       next
       if lst=empty then
           Arr=Array()
       else
           if left(lst,1)=SP then lst=right(lst,len(lst)-1)
           Arr=split(lst,SP)
       end if
   end sub
   
   public sub test()
       for each a in arr
           response.Write("<br/>" &typename(a) &" | " &a)
       next
       response.Write("<hr/>" &ubound(arr))
   end sub
end class


'''示例'''''''''''''
set al=new CLS_ArrayList
al.add "1234"
al.add 2
al.add 5.6
al.add 9999999999999876.6131
al.add "9999999999999321.6879"

al.add false
al.add empty
al.add vbnull
al.add null
al.add date
al.add time
al.add now
al.add 999999999999
al.add "ssss"
al.addkey "ssss"
al.addkey "abcd"
al.Modify "tttt","sSsSsS"


response.write al.test
al.KeepInt
response.write al.test

set al=nothing
%>