两组字符串数据比较合并相同数据(2)
作者:cnbruce 来源:cnbruce blog 时间:2008-07-31 17:27:00
如何解决不再运算比较已经被比较运算过的元素
我们必须对已经比较运算过的元素进行标记,比如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
%>
测试(分别调整输入a1和a2对应的值测试最终效果)
标签:字符串,数据,字符
0
投稿
猜你喜欢
Python实现PIL图像处理库绘制国际象棋棋盘
2021-06-20 14:18:08
Asp性能优化之Response.IsClientConnected属性及其应用示例
2008-09-18 12:13:00
Python pomegranate库实现基于贝叶斯网络拼写检查器
2021-06-17 19:27:51
python计算机视觉opencv图像金字塔轮廓及模板匹配
2021-10-10 09:15:33
javascript实现炫酷的拖动分页
2024-06-19 01:46:04
BootstrapValidator超详细教程(推荐)
2024-04-10 13:53:24
Python 使用csv库处理CSV文件的方法
2023-03-16 21:37:24
python实现决策树分类
2021-04-04 13:35:28
PHP中重启php-fpm的几种方法汇总
2023-06-12 21:05:24
实例讲解使用原生JavaScript处理AJAX请求的方法
2024-04-16 08:47:16
MySQL索引的基本语法
2024-01-26 00:48:44
彻底搞懂MySQL存储过程和函数
2024-01-24 10:48:52
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2023-04-12 03:34:40
全方位清理浮动
2009-06-16 14:51:00
Python下载ts文件视频且合并的操作方法
2021-11-15 15:40:19
python入门之算法学习
2021-05-16 19:38:19
Python自定义线程池实现方法分析
2021-12-17 09:13:10
Python tkinter制作单机五子棋游戏
2021-08-01 03:31:43
Pygame Transform图像变形的实现示例
2022-03-04 03:39:29
详解tensorflow训练自己的数据集实现CNN图像分类
2023-02-28 10:47:04