蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年8月25日 8:28

[ASP]数据比较问题

有两组数据,每组里面有七个数据,要比较后的结果:知道这两组里面有几个数相同;

A组:1,2,3,4,5,6,7

B组:9,5,6,7,2,3,4

如何比较并得到相同结果的数据?比如说这一组就应该是有:6个数相同。要的就是这个结果,6个数据相同。

大家给点思路,没做过这样的。

循环其中一组,将单个去与另一组的查找比较,若包含则加1次。

大哥,发现你总是出题给别人做啊,要写程序不能总是这样的风格啊
这有什么思路不思路的
只能循环了
[html]
<script language="vbscript">
arrNum1 = split("1,2,3,4,5,6,7",",")
arrNum2 = split("9,5,6,7,2,3,4",",")
intCount = 0
        for i = 0 to ubound(arrNum1)
                for j = 0 to ubound(arrNum2)
                        if arrNum1(i) = arrNum2(j) then
                                intCount = intCount + 1
                        end if
                next
        next
msgbox ("有"&intCount&"个数相同!")
</script>
[/html]

借花献佛:D
[html]<script language="vbscript">
arrNum1 = "1,2,3,4,5,6,7"
arrNum2 = split("9,5,6,7,2,3,4",",")
intCount = 0
for i = 0 to ubound(arrNum2)
        if instr("," & arrNum1 & ",","," & arrNum2(i) & ",")>0 then
                intCount = intCount + 1
        end if
next
msgbox ("有"&intCount&"个数相同!")
</script>[/html]

谢谢大家;

TO:maypaopao  不好意思,我测试了一下午,结果总是1,看了你们写的比我的又简单,还要清楚,
if instr("," & arrNum1 & ",","," & arrNum2(i) & ",")>0 then
这地方不一样,你们这样写没太清楚是怎么比较的?

我原来想的:先定义两个变量A与B,然后把A中的数全读取出来,存到另外一个变量里面,然后再去循环去与B比较,测试结果总是1个.

楼上的好像比3楼的循环少一个?是不是少一个对程序的性能来说要好一些?


instr("[color=#ff0000]," & arrNum1 & ",[/color]","[color=#0000ff]," & arrNum2(i) & ",[/color]")

这样看就比较容易看明白了~~

1,2,3,4,5,6,7
变成
,1,2,3,4,5,6,7,
把循环中的
2
变成
,2,
再在“,1,2,3,4,5,6,7,”中查找比较是否包含“,2,”

也就是都在左右加上单引事号这样比较就比较准确了~~当然就你目前的例子没有必要,如果有12,23此类的数字就要加上比较出来的结果才是正确的~~~自己想一下就明白了。

谢谢楼上的,正是这种意思.呵呵.

我最开始的意思,实际上是这样的.
数据库的表A里面有字段B,B的值就是类似上面的.
另外有一组数据是固定的C,
从表A中选择出,与C位数有5个相同的数据.

[code]

dim num1,num2,num3,zs
num1="20,31,5,19,31,10,9" '固定的一组数据
Set rs= Server.CreateObject("ADODB.Recordset")
sql="select nums from numbers"
rs.open sql,conn,1,1
do while not rs.eof
num3=rs("nums")
num2=split(""&num3&"",",")  '库中读取的数据 格式也是七个数据,中间有逗号隔开
response.write num2

;;;;;;;;;;;;;;;;;;; [color=#FF0000]这里就不知道怎么写了[/color]

rs.movenext
loop
[/code]


不明白你是每一条记录都是用逗号隔开然后要将所有的记录都和已知的数据比较一次呢

还是数据库中每一条记录是单个数字,然后用逗号串起来再和已知的数据进行比较

是这样的:表A中有一字段B,B的值就是和上面的是一样,也是里面有七个数字(中间有逗号隔开);当然A表中可能有多条记录,

然后有一组固定的值C (里面也是有七个数字,中间用逗号隔开);

现在要判断表A中有多少组数据与C相比较后,结果有5个是相同的.

不知道我没有表达清楚.

举例就是这样的,比如A表中有12组数据,
X1=1,2,3,4,5,6,7
.....
X12=5,9,8,12,35,10,9
这样12组,(当然A表中的数据不是固定的,随时变化的)

与固定的值C进行比较,判断一下有相同位数为5的一共有多少条这样的记录;有多少条记录与C进行比较之后有5位相同的数据;

大概的意思就是这样的.

[code]<%
dim num1,num2,num3,zs
num1 = "20,31,5,19,31,10,9"        '固定的一组数据
zs   = 0
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "select nums from numbers"
rs.open sql,conn,1,1
        do while not rs.eof
                num2 = rs("nums")
                num3 = intCount(num1,num2)
                response.write(num2 & "---" & num3 & "<br />" & vbcrlf)
                if num3=5 then zs = zs+1
                rs.movenext
        loop
rs.close
set rs = nothing

response.write("有 [" & zs & "] 条记录与num1进行比较之后有5位相同的数据")

Function intCount(str1,str2)
        dim str,i
        str = split(str1,",")
        intCount = 0
        for i = 0 to ubound(str)
                if instr("," & str2 & ",","," & str(i) & ",")>0 then
                        intCount = intCount + 1
                end if
        next
End Function
%>[/code]
未作测试,若有错请自行修改或写出错误提示。

Microsoft VBScript 运行时错误 错误 '800a000d'

类型不匹配: 'str1'

num2 = rs("nums")
我在表里面设置的nums字段类型是文本型的.

应该没有问题呀,你是直接复制的还是修改过?

实在不行给出你的数据库来,我测试好了给你代码~

数据库名:2.mdb  表名是:numbers 里面有字段:id(自动编号),nums(文本型),
我的代码:
[code]
<%
Class DataBaseClass
Private IConnStr

Public Property Let ConnStr(Val)
    IConnStr = Val
End Property

Public Property Get ConnStr()
    ConnStr = IConnStr
End Property

Private Sub Class_initialize()
ConnStr = "DBQ=" + Server.MapPath("2.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
End Sub

Private Sub Class_Terminate()
        ConnStr = Null
End Sub

Public Function OpenConnection()
        Dim TempConn
        Set TempConn = Server.CreateObject("ADODB.Connection")
        TempConn.Open ConnStr
        Set OpenConnection = TempConn
        Set TempConn = Nothing
        if Err.Number <> 0 then
                Response.Write("<script>alert('[系统错误]\n\n数据库连接错误!请检查数据库配置文件!');window.close()</script>")  
        Response.End
        end if
End Function
End Class
Dim DBC,Conn
Set DBC = New DataBaseClass
Set Conn = DBC.OpenConnection()
Set DBC = Nothing

dim num1,num2,num3,zs
num1 = "20,31,5,19,31,10,9"        '固定的一组数据
zs   = 0
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "select nums from numbers"
rs.open sql,conn,1,1
        do while not rs.eof
                num2 = rs("nums")
                num3 = intCount(num1,num2)
                response.write(num2 & "---" & num3)
                if num3=3 then zs = zs+1
                rs.movenext
        loop
rs.close
set rs = nothing

response.write("有 [" & zs & "] 条记录与num1进行比较之后有5位相同的数据")
Function intCount(str1,str2)
        str1 = split(str1,",")
        dim i
        intCount = 0
        for i = 0 to ubound(str1)
                if instr("," & str2 & ",","," & str1(i) & ",")>0 then
                        intCount = intCount + 1
                end if
        next
End Function
%>[/code]

是我变量名没有取好,现在好了(加了一个变量),我编辑过代码~~见10楼

谢谢楼上的.变量名不声明就会出错吗?

小强  我顶

曾经的我在上个帖子中说


目前代码不是,没有写强制声明。只是我想偷懒,少定义了个变量,重新赋值时就出错了。

谢谢楼上的,问题已经解决了.

我后来说的效果,再加判断条件就可以实现的.




Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com