两组字符串数据比较合并相同数据
2008-10-26 23:33
501 查看
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
//-->
</SCRIPT>
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</SCRIPT>
<SCRIPT> window.google_render_ad(); </SCRIPT>
<IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4490194096475053&dt=1225035206468&lmt=1219829509&prev_slotnames=1891601125&output=html&slotname=3685991503&correlator=1225035206437&url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F08%2F1418.html&ea=0&ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__71.html&frm=0&ga_vid=829866576.1224664711&ga_sid=1225034957&ga_hid=1806175700&ga_fc=true&flash=9.0.124.0&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&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,";")通过for i = 0 to ubound(s_array)循环我们可以获得单独的各项数据及值
其中每项的格式是类似“sp2=20”,要将sp2提取出来才能和同组中的数据进行比较,所以还需要独立函数进行提取
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
分别获得“=”前的数据名称和“=”后的数据值。
其次每块数据都分解下来了,就是如何寻找到相同的数据名称。
我们假设这样的流程,首先将a3数组中的第一元素提取,通过和除第一元素之前以为的数据进行比较,如果有相同则进行相加。
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
next
next
我们获得了最终的值可以随时将值赋到新的动态数组中,组合成最终的“组合数据”数组
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
即
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
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
Nums = getSPNum(s_array(i))
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)))
redim Preserve ID(q)
ID(q) = j
q = q + 1
end if
next
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
p = p + 1
end if
next
for each x in result
mainhb=mainhb&x&";"
next
end function
整体函数为
<%
dim result()
dim ID()
dim p , q , Nums
p=0
q= 0
Nums = 0
redim Preserve ID(q)
ID(q) = ""
s = "sp4=33;sp2=20;sp1=34;sp3=2;sp2=3;sp4=4;"
s = left(s,len(s)-1)
response.write mainhb(s)
function mainhb(s)
s_array = split(s,";")
for i = 0 to ubound(s_array)
if not IsInID(i) then
Nums = getSPNum(s_array(i))
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)))
redim Preserve ID(q)
ID(q) = j
q = q + 1
end if
next
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
p = p + 1
end if
'Nums = 0
next
for each x in result
mainhb=mainhb&x&";"
next
end function
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
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
%>
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
//-->
</SCRIPT>
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</SCRIPT>
<SCRIPT> window.google_render_ad(); </SCRIPT>
<IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4490194096475053&dt=1225035206468&lmt=1219829509&prev_slotnames=1891601125&output=html&slotname=3685991503&correlator=1225035206437&url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F08%2F1418.html&ea=0&ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__71.html&frm=0&ga_vid=829866576.1224664711&ga_sid=1225034957&ga_hid=1806175700&ga_fc=true&flash=9.0.124.0&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&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,";")通过for i = 0 to ubound(s_array)循环我们可以获得单独的各项数据及值
其中每项的格式是类似“sp2=20”,要将sp2提取出来才能和同组中的数据进行比较,所以还需要独立函数进行提取
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
分别获得“=”前的数据名称和“=”后的数据值。
其次每块数据都分解下来了,就是如何寻找到相同的数据名称。
我们假设这样的流程,首先将a3数组中的第一元素提取,通过和除第一元素之前以为的数据进行比较,如果有相同则进行相加。
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
next
next
我们获得了最终的值可以随时将值赋到新的动态数组中,组合成最终的“组合数据”数组
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
即
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
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
Nums = getSPNum(s_array(i))
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)))
redim Preserve ID(q)
ID(q) = j
q = q + 1
end if
next
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
p = p + 1
end if
next
for each x in result
mainhb=mainhb&x&";"
next
end function
整体函数为
<%
dim result()
dim ID()
dim p , q , Nums
p=0
q= 0
Nums = 0
redim Preserve ID(q)
ID(q) = ""
s = "sp4=33;sp2=20;sp1=34;sp3=2;sp2=3;sp4=4;"
s = left(s,len(s)-1)
response.write mainhb(s)
function mainhb(s)
s_array = split(s,";")
for i = 0 to ubound(s_array)
if not IsInID(i) then
Nums = getSPNum(s_array(i))
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)))
redim Preserve ID(q)
ID(q) = j
q = q + 1
end if
next
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & "=" & Nums
p = p + 1
end if
'Nums = 0
next
for each x in result
mainhb=mainhb&x&";"
next
end function
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
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
%>
相关文章推荐
- 两组字符串数据比较合并相同数据
- 两组字符串数据比较合并相同数据
- 两组字符串数据比较合并相同数据
- asp两组字符串数据比较合并相同数据
- asp两组字符串数据比较合并相同数据
- [转载]excel怎么比较两组或两列数据的相同项和不同项
- Excel 比较出两组数据不相同的
- 两个文件比较求交集或合并相同的数据
- C#中使用Equals()函数比较两个字符串是否相同
- 表格相同单元格数据进行合并
- 比较两个字符串,找出最大的相同子串
- Repeter中列相同数据合并
- 快速比较两个字符串中字符完全相同(兄弟字符串比较)
- PHP两个字符串比较(人为出错),两字符串类型和数据表面相等,但strcmp()结果不为0
- leetcode_318. Maximum Product of Word Lengths 求两个不相交的字符串的长度乘积的最大值,将字母转换成二进制形式,按位与比较是否有相同字母
- 数据库中多行数据合并成一个字符串
- 解决C/C++下相同字符串比较结果不为0
- mysql多条相同连接的数据合并一条函数
- 编写一个密码类,其中包含一个 str 密码字符串私有成员数据,一个“==”运算 符重载成员函数,用于比较用户输入的密码是否正确。并用数据测试该类。
- 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。