逗号分隔列表的经典ASP比较

时间:2010-06-16 10:19:28

标签: regex arrays asp-classic vbscript

我有两个以逗号分隔的列表: -

36,189,47,183,65,50

65,50,189,47

问题是如何比较经典ASP中的两个,以便识别和返回列表1中存在但列表2中不存在的任何值,同时牢记关联数组不可用。

,例如,在上面的示例中,我需要返回值为36,183

由于

3 个答案:

答案 0 :(得分:0)

像这样(未经测试):

str1 = "36,189,47,183,65,50"
str2 = "65,50,189,47"

arr1 = Split(str1, ",")
arr2 = Split(str2, ",")

for i = 0 to UBound(arr1)  
   found = false
   for j = 0 to UBound(arr2)
      if arr1(i) = arr2(j) then
         found = true
      end if
   next   
   if found = false then
      Response.Write(arr1(i))
   end if
next

答案 1 :(得分:0)

为了使用正则表达式解决此问题,您可以使用前瞻(正面和负面)和引用,例如:

(zyx:~) % echo '36,189,47,183,65,50;65,50,189,47' | grep -oP '((?>(?<![^,;])[^,;]+))(?=.*;)(?!.*;(|.*,)\1(?=,|$))'
36
183

其他变体(适用于PCRE,但不适用于perl):

(zyx:~) % echo '36,189,47,183,65,50' | grep -oP '((?!(?<=,|^)65|50|189|47(?=,|$))(?<=,|^)[^,]+(?=,|$))'
36
183

不知道这些是否适用于asp。

答案 2 :(得分:0)

VBScript具有Dictionary对象形式的关联数组。

Dim list1, list2
list1 = "36,189,47,183,65,50"
list2 = "65,50,189,47"

Dim arr1, arr2
arr1 = Split(list1, ",")
arr2 = Split(list2, ",")

' oDict will hold values from list1
Dim oDict, i
Set oDict = Server.CreateObject("Scripting.Dictionary")

For i = 0 To UBound(arr1)
    oDict(arr1(i)) = 1
Next

' Now loop through list2 and remove matching items from list1
For i = 0 To UBound(arr2)
    If oDict.Exists(arr2(i)) Then
        oDict.Remove arr2(i)
    End If
Next

Response.Write Join(oDict.Keys, ",") ' should be "36,183"