两组字符串数据比较合并相同数据

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4490194096475053&amp;dt=1225035206468&amp;lmt=1219829509&amp;prev_slotnames=1891601125&amp;output=html&amp;slotname=3685991503&amp;correlator=1225035206437&amp;url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F08%2F1418.html&amp;ea=0&amp;ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__71.html&amp;frm=0&amp;ga_vid=829866576.1224664711&amp;ga_sid=1225034957&amp;ga_hid=1806175700&amp;ga_fc=true&amp;flash=9.0.124.0&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="300" scrolling="no" height="250" allowtransparency></iframe> 两组字符串数据,需要比较其中相同的数据,并将其值相加并组成一个新的字符串数据

a1="sp2=20;sp1=34;"
a2="sp3=2;sp2=3;sp1=4;"
两组字符串数据,将字符串中相同的数据值相加后得到新的一组数据
即“sp3=2;sp2=23;sp1=38”

(p.s一个简单的应用:商品二原有数量20件,商品一原有数量34件,新进货或者新出售了商品二3件,商品一4件等类型模拟情况下计算出进货量,销售量和库存量,小型的进销存系统可采用这样的方法)

那么如何实现两组字符串数据比较合并相同数据?

第一,将两组字符串数据进行连接组合

a3=a1&a2
那么a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;"

第二,将a3中相同的数据进行值的相加

这里主要解决的是如何寻找到相同的数据

首先因为现在a3就是由sp2、sp1、sp3、sp2和sp1组成,需要把相同的sp2和sp1单独找出来再进行值得相加。

通过split函数分割“;”为分隔符获得每块数据和值。
即s_array=split(a3,";")通过fori=0toubound(s_array)循环我们可以获得单独的各项数据及值

其中每项的格式是类似“sp2=20”,要将sp2提取出来才能和同组中的数据进行比较,所以还需要独立函数进行提取

FunctiongetSPName(sp)
getSPName=split(sp,"=")(0)
EndFunction

FunctiongetSPNum(sp)
getSPNum=split(sp,"=")(1)
endfunction

分别获得“=”前的数据名称和“=”后的数据值。

其次每块数据都分解下来了,就是如何寻找到相同的数据名称。
我们假设这样的流程,首先将a3数组中的第一元素提取,通过和除第一元素之前以为的数据进行比较,如果有相同则进行相加。


s_array=split(a3,";")
fori=0toubound(s_array)
forj=i+1toubound(s_array)
ifgetSPName(s_array(i))=getSPName(s_array(j))then
Nums=Nums+Cint(getSPNum(s_array(j)))
endif
next
next



我们获得了最终的值可以随时将值赋到新的动态数组中,组合成最终的“组合数据”数组
redimPreserveresult(p)
result(p)=getSPName(s_array(i))&"="&Nums



s_array=split(a3,";")
fori=0toubound(s_array)
forj=i+1toubound(s_array)
ifgetSPName(s_array(i))=getSPName(s_array(j))then
Nums=Nums+Cint(getSPNum(s_array(j)))
endif
next

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




这个里面势必会遇到这样的一个情况:当a3数组中的其后的某一元素总会与之前比较的相同的元素进行了运算,所以该元素就不能计入fori=0toubound(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,";")
fori=0toubound(s_array)
forj=i+1toubound(s_array)
ifgetSPName(s_array(i))=getSPName(s_array(j))then
Nums=Nums+Cint(getSPNum(s_array(j)))
endif

redimPreserveID(q)
ID(q)=j
q=q+1
next

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



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


functionIsInID(j)
dimx
IsInID=false
foreachxinID
ifx=jthen
IsInID=true
exitfunction
Endif
Next
endfunction




主要函数为


functionmainhb(s)
s_array=split(s,";")
fori=0toubound(s_array)
ifnotIsInID(i)then
Nums=getSPNum(s_array(i))
forj=i+1toubound(s_array)
ifgetSPName(s_array(i))=getSPName(s_array(j))then
Nums=Nums+Cint(getSPNum(s_array(j)))
redimPreserveID(q)
ID(q)=j
q=q+1
endif
next

redimPreserveresult(p)
result(p)=getSPName(s_array(i))&"="&Nums
p=p+1
endif
next

foreachxinresult
mainhb=mainhb&x&";"
next
endfunction




整体函数为


<%
dimresult()
dimID()
dimp,q,Nums

p=0
q=0
Nums=0

redimPreserveID(q)
ID(q)=""

s="sp4=33;sp2=20;sp1=34;sp3=2;sp2=3;sp4=4;"
s=left(s,len(s)-1)
response.writemainhb(s)

functionmainhb(s)
s_array=split(s,";")
fori=0toubound(s_array)
ifnotIsInID(i)then
Nums=getSPNum(s_array(i))
forj=i+1toubound(s_array)
ifgetSPName(s_array(i))=getSPName(s_array(j))then
Nums=Nums+Cint(getSPNum(s_array(j)))
redimPreserveID(q)
ID(q)=j
q=q+1
endif
next

redimPreserveresult(p)
result(p)=getSPName(s_array(i))&"="&Nums
p=p+1
endif
'Nums=0
next

foreachxinresult
mainhb=mainhb&x&";"
next
endfunction

FunctiongetSPName(sp)
getSPName=split(sp,"=")(0)
EndFunction


FunctiongetSPNum(sp)
getSPNum=split(sp,"=")(1)
endfunction

functionIsInID(j)
dimx
IsInID=false
foreachxinID
ifx=jthen
IsInID=true
exitfunction
Endif
Next
endfunction
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值