asp兩組字符串?dāng)?shù)據(jù)比較合并相同數(shù)據(jù)
更新時(shí)間:2008年08月06日 09:44:41 作者:
兩組字符串?dāng)?shù)據(jù),需要比較其中相同的數(shù)據(jù),并將其值相加并組成一個(gè)新的字符串?dāng)?shù)據(jù)
a1="sp2=20;sp1=34;"
a2="sp3=2;sp2=3;sp1=4;"
兩組字符串?dāng)?shù)據(jù),將字符串中相同的數(shù)據(jù)值相加后得到新的一組數(shù)據(jù)
即“sp3=2;sp2=23;sp1=38”
(p.s 一個(gè)簡(jiǎn)單的應(yīng)用:商品二原有數(shù)量20件,商品一原有數(shù)量34件,新進(jìn)貨或者新出售了商品二3件,商品一4件等類型模擬情況下計(jì)算出進(jìn)貨量,銷(xiāo)售量和庫(kù)存量,小型的進(jìn)銷(xiāo)存系統(tǒng)可采用這樣的方法)
那么如何實(shí)現(xiàn)兩組字符串?dāng)?shù)據(jù)比較合并相同數(shù)據(jù)?
第一,將兩組字符串?dāng)?shù)據(jù)進(jìn)行連接組合
a3=a1&a2
那么a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;"
第二,將a3中相同的數(shù)據(jù)進(jìn)行值的相加
這里主要解決的是如何尋找到相同的數(shù)據(jù)
首先因?yàn)楝F(xiàn)在a3就是由 sp2、sp1、sp3、sp2和sp1組成,需要把相同的sp2和sp1單獨(dú)找出來(lái)再進(jìn)行值得相加。
通過(guò)split函數(shù)分割“;”為分隔符獲得每塊數(shù)據(jù)和值。
即 s_array = split(a3,";")通過(guò)for i = 0 to ubound(s_array)循環(huán)我們可以獲得單獨(dú)的各項(xiàng)數(shù)據(jù)及值
其中每項(xiàng)的格式是類似“sp2=20”,要將sp2提取出來(lái)才能和同組中的數(shù)據(jù)進(jìn)行比較,所以還需要獨(dú)立函數(shù)進(jìn)行提取
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
分別獲得“=”前的數(shù)據(jù)名稱和“=”后的數(shù)據(jù)值。
其次每塊數(shù)據(jù)都分解下來(lái)了,就是如何尋找到相同的數(shù)據(jù)名稱。
我們假設(shè)這樣的流程,首先將a3數(shù)組中的第一元素提取,通過(guò)和除第一元素之前以為的數(shù)據(jù)進(jìn)行比較,如果有相同則進(jìn)行相加。
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
我們獲得了最終的值可以隨時(shí)將值賦到新的動(dòng)態(tài)數(shù)組中,組合成最終的“組合數(shù)據(jù)”數(shù)組
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
這個(gè)里面勢(shì)必會(huì)遇到這樣的一個(gè)情況:當(dāng)a3數(shù)組中的其后的某一元素總會(huì)與之前比較的相同的元素進(jìn)行了運(yùn)算,所以該元素就不能計(jì)入 for i = 0 to ubound(s_array)內(nèi)的result(p) = getSPName(s_array(i)) & "=" & Nums動(dòng)態(tài)數(shù)組中去。
如何解決不再運(yùn)算比較已經(jīng)被比較運(yùn)算過(guò)的元素
我們必須對(duì)已經(jīng)比較運(yùn)算過(guò)的元素進(jìn)行標(biāo)記,比如a3數(shù)組中(a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;")取出sp2=20后會(huì)比較運(yùn)算到后一個(gè)sp2=3,此時(shí)比較運(yùn)算后將sp2=3的數(shù)組元素編號(hào)進(jìn)行標(biāo)記,下次循環(huán)比較時(shí)該元素不計(jì)在內(nèi)。
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就是將當(dāng)前比較相同的該元素標(biāo)記,并賦值于動(dòng)態(tài)數(shù)組id(q),q默認(rèn)定義為0,再次循環(huán)q=q+1
那么有力該標(biāo)記,我們就可以有選擇性的選擇比較累加了。
定義函數(shù)
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
主要函數(shù)為
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
整體函數(shù)為
<%
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
%>
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
a2="sp3=2;sp2=3;sp1=4;"
兩組字符串?dāng)?shù)據(jù),將字符串中相同的數(shù)據(jù)值相加后得到新的一組數(shù)據(jù)
即“sp3=2;sp2=23;sp1=38”
(p.s 一個(gè)簡(jiǎn)單的應(yīng)用:商品二原有數(shù)量20件,商品一原有數(shù)量34件,新進(jìn)貨或者新出售了商品二3件,商品一4件等類型模擬情況下計(jì)算出進(jìn)貨量,銷(xiāo)售量和庫(kù)存量,小型的進(jìn)銷(xiāo)存系統(tǒng)可采用這樣的方法)
那么如何實(shí)現(xiàn)兩組字符串?dāng)?shù)據(jù)比較合并相同數(shù)據(jù)?
第一,將兩組字符串?dāng)?shù)據(jù)進(jìn)行連接組合
a3=a1&a2
那么a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;"
第二,將a3中相同的數(shù)據(jù)進(jìn)行值的相加
這里主要解決的是如何尋找到相同的數(shù)據(jù)
首先因?yàn)楝F(xiàn)在a3就是由 sp2、sp1、sp3、sp2和sp1組成,需要把相同的sp2和sp1單獨(dú)找出來(lái)再進(jìn)行值得相加。
通過(guò)split函數(shù)分割“;”為分隔符獲得每塊數(shù)據(jù)和值。
即 s_array = split(a3,";")通過(guò)for i = 0 to ubound(s_array)循環(huán)我們可以獲得單獨(dú)的各項(xiàng)數(shù)據(jù)及值
其中每項(xiàng)的格式是類似“sp2=20”,要將sp2提取出來(lái)才能和同組中的數(shù)據(jù)進(jìn)行比較,所以還需要獨(dú)立函數(shù)進(jìn)行提取
Function getSPName(sp)
getSPName = split(sp,"=")(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,"=")(1)
end function
分別獲得“=”前的數(shù)據(jù)名稱和“=”后的數(shù)據(jù)值。
其次每塊數(shù)據(jù)都分解下來(lái)了,就是如何尋找到相同的數(shù)據(jù)名稱。
我們假設(shè)這樣的流程,首先將a3數(shù)組中的第一元素提取,通過(guò)和除第一元素之前以為的數(shù)據(jù)進(jìn)行比較,如果有相同則進(jìn)行相加。
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
我們獲得了最終的值可以隨時(shí)將值賦到新的動(dòng)態(tài)數(shù)組中,組合成最終的“組合數(shù)據(jù)”數(shù)組
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
這個(gè)里面勢(shì)必會(huì)遇到這樣的一個(gè)情況:當(dāng)a3數(shù)組中的其后的某一元素總會(huì)與之前比較的相同的元素進(jìn)行了運(yùn)算,所以該元素就不能計(jì)入 for i = 0 to ubound(s_array)內(nèi)的result(p) = getSPName(s_array(i)) & "=" & Nums動(dòng)態(tài)數(shù)組中去。
如何解決不再運(yùn)算比較已經(jīng)被比較運(yùn)算過(guò)的元素
我們必須對(duì)已經(jīng)比較運(yùn)算過(guò)的元素進(jìn)行標(biāo)記,比如a3數(shù)組中(a3="sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;")取出sp2=20后會(huì)比較運(yùn)算到后一個(gè)sp2=3,此時(shí)比較運(yùn)算后將sp2=3的數(shù)組元素編號(hào)進(jìn)行標(biāo)記,下次循環(huán)比較時(shí)該元素不計(jì)在內(nèi)。
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就是將當(dāng)前比較相同的該元素標(biāo)記,并賦值于動(dòng)態(tài)數(shù)組id(q),q默認(rèn)定義為0,再次循環(huán)q=q+1
那么有力該標(biāo)記,我們就可以有選擇性的選擇比較累加了。
定義函數(shù)
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
主要函數(shù)為
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
整體函數(shù)為
<%
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
%>
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
相關(guān)文章
ASP中實(shí)現(xiàn)分頁(yè)顯示的七種方法
這篇文章主要介紹了ASP中實(shí)現(xiàn)分頁(yè)顯示的七種方法,這七種方法可以分為四大類,需要的朋友可以參考下2015-09-09
asp MYSQL出現(xiàn)問(wèn)號(hào)亂碼的解決方法
用asp讀取MYSQL數(shù)據(jù)庫(kù)出現(xiàn)亂碼,讀取到的漢字都是????2008-08-08
ASP常見(jiàn)錯(cuò)誤詳解及解決方案小結(jié) 推薦
ASP是非常簡(jiǎn)單的,以至于許多的開(kāi)發(fā)者不會(huì)去思考錯(cuò)誤處理。錯(cuò)誤處理能夠讓你的應(yīng)用程序更加合理。我看到過(guò)很多個(gè)用ASP編寫(xiě)的商業(yè)網(wǎng)站,大多數(shù)都忽略了錯(cuò)誤處理。 錯(cuò)誤的類型。2011-01-01
ByVal和ByRef(編寫(xiě)ASP子程序所用到命令)
ByRef 傳送參數(shù)內(nèi)存的實(shí)際地址給被調(diào)用者。也就是說(shuō),棧中壓入的是實(shí)際內(nèi)容的地址。被調(diào)用者可以直接更改該地址中的內(nèi)容。2006-08-08
迅雷API接口_通過(guò)腳本調(diào)用迅雷自動(dòng)下載資源
最近在寫(xiě)一個(gè)采集程序,需要下載目標(biāo)站的附件,不過(guò)目標(biāo)站文件下載速度很慢,于是想到能否調(diào)用迅雷來(lái)下載2008-07-07
通過(guò)MSXML2自動(dòng)獲取QQ個(gè)人頭像及在線情況(給初學(xué)者)
通過(guò)MSXML2自動(dòng)獲取QQ個(gè)人頭像及在線情況(給初學(xué)者)...2007-03-03

