asp两组字符串数据比较合并相同数据(2)

来源:asp之家 时间:2011-04-14 11:08: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 
%> 

 

标签:asp,字符串,数据比较,合并
0
投稿

猜你喜欢

  • 静态页面利用JS读取cookies记住用户信息

    2011-04-14 11:17:00
  • 实用技巧:优化SQL Server数据库查询方法

    2009-02-04 13:46:00
  • 带进度条的ASP无组件断点续传大文件下载

    2010-06-25 18:27:00
  • sql 存储过程批量删除数据的语句

    2012-08-21 10:24:14
  • 数据库技巧——MySQL十大优化技巧

    2011-01-31 16:44:00
  • asp如何实现对Session 数组的定义和调用?

    2010-05-18 18:40:00
  • Orcas中C#语言的新特性:自动属性,对象初始化器,和集合初始化器

    2007-09-23 12:43:00
  • 在SQL查询中使用LIKE来代替IN查询的方法

    2011-09-30 11:10:18
  • 对“关于购物车的想法”的一些回复

    2009-03-10 18:15:00
  • jQuery.animate简单分析

    2010-06-26 12:45:00
  • SWFObject2.0: 基于Javascript的Flash媒体版本检测与嵌入模块

    2009-08-19 13:24:00
  • 介绍27款经典的CSS框架

    2011-03-04 16:24:00
  • 解析xml字符串的函数

    2008-06-10 12:37:00
  • Dreamweaver虚拟在线试衣室

    2009-07-05 18:54:00
  • 在Oracle PL/SQL中游标声明中表名动态变化的方法

    2009-02-28 10:39:00
  • js验证表单(form)中的单选(radio)值

    2008-03-18 13:23:00
  • 你知道怎么在淘宝里进行投诉吗?

    2008-06-04 12:00:00
  • 成为一个顶级设计师的第一准则

    2008-04-18 10:29:00
  • asp如何计算下载一个文件需要多长时间?

    2009-11-25 20:17:00
  • AJAX无刷新验证用户名是否存在

    2007-08-10 10:07:00
  • asp之家 网络编程 m.aspxhome.com