Classic ASP comparison of comma separated lists
I have two comma separated lists:-
36,189,47,183,65,50
65,50,189,47
The question is how to compare the two in classic ASP in order to identify and return any values that exist in list 1 but that don't exist in list 2 bearing in mind that associative arrays aren't available.
E.g., in the above example I would need the return value to be 36,183
Thanks
Something like this (untested):
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
In order to solve this with regular expression you can use lookaheads (both positive and negative) and references, for example:
(zyx:~) % echo '36,189,47,183,65,50;65,50,189,47' | grep -oP '((?>(?<![^,;])[^,;]+))(?=.*;)(?!.*;(|.*,)\1(?=,|$))'
36
183
Other variant (works in PCRE but not in perl):
(zyx:~) % echo '36,189,47,183,65,50' | grep -oP '((?!(?<=,|^)65|50|189|47(?=,|$))(?<=,|^)[^,]+(?=,|$))'
36
183
Have no idea whether any of these works in asp.
VBScript has associative arrays in the form of the Dictionary object.
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"
精彩评论