国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

asp兩組字符串數據比較合并相同數據

2024-05-04 11:09:16
字體:
來源:轉載
供稿:網友
兩組字符串數據,需要比較其中相同的數據,并將其值相加并組成一個新的字符串數據
 
 
 
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 
%> 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐至县| 德惠市| 成都市| 渑池县| 克拉玛依市| 庐江县| 两当县| 兴文县| 双江| 钟山县| 慈利县| 中阳县| 湘潭县| 金堂县| 平安县| 吉水县| 莎车县| 辽中县| 阳原县| 肇源县| 都兰县| 濮阳县| 德惠市| 大竹县| 雷州市| 大同县| 阿尔山市| 石城县| 漳浦县| 玛纳斯县| 阜新市| 潼南县| 乐都县| 尉氏县| 岐山县| 静安区| 洪雅县| 沿河| 泾川县| 水富县| 九寨沟县|