asp两组字符串数据比较合并相同数据(2)


        end if
    next

    redim Preserve result(p)
    result(p) = getSPName(s_array(i)) & "=" & Nums
    p=p+1
next

 


这个里面势必会遇到这样的一个情况:当a3数组中的其后的某一元素总会与之前比较的相同的元素进行了运算,所以该元素就不能计入 for i = 0 to ubound(s_array)内的result(p) = getSPName(s_array(i)) & "=" & Nums动态数组中去。

如何解决不再运算比较已经被比较运算过的元素

我们必须对已经比较运算过的元素进行标记,比如a3数组中(a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;")取出sp2=20后会比较运算到后一个sp2=3,此时比较运算后将sp2=3的数组元素编号进行标记,下次循环比较时该元素不计在内。


s_array = split(a3,";")
for i = 0 to ubound(s_array)
    for j=i+1 to ubound(s_array)
        if getSPName(s_array(i)) = getSPName(s_array(j)) then
        Nums = Nums + Cint(getSPNum(s_array(j)))
        end if

        redim Preserve ID(q)
        ID(q) = j
        q = q + 1
    next

    redim Preserve result(p)
    result(p) = getSPName(s_array(i)) & "=" & Nums
    p=p+1
next

 

其中定义ID(q)=j就是将当前比较相同的该元素标记,并赋值于动态数组id(q),q默认定义为0,再次循环q=q+1
那么有力该标记,我们就可以有选择性的选择比较累加了。
定义函数


function IsInID(j)
    dim x
    IsInID = false
    for each x in ID
        if x = j then 
            IsInID = true
            exit function
        End if
    Next
end function

 


主要函数为


function mainhb(s)
s_array = split(s,";")
    for i = 0 to ubound(s_array)
        if not IsInID(i) then

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2548.html