border image
logo border image

Loading






Your basket has: Total items: 0
Subtotal: $0.00




border image
 
corner image
corner image

Article


 Comparing two sets of data
Sometimes its necessary to compare two sets of data to find common or uncommon values. This script compares two arrays, giving the user the option to choose which comparisison to view.



<%@ LANGUAGE="VBSCRIPT" %>

<%
    Response.Buffer=true
    Response.AddHeader "cache-control", "private"
    Response.AddHeader "pragma", "no-cache"
    Response.ExpiresAbsolute = #January 1, 1990 00:00:01#
    Response.Expires=-1

'------------------------------------------------------------------
'This page calls itself so get file name
'------------------------------------------------------------------

strScriptName = Request.ServerVariables("SCRIPT_NAME")

'------------------------------------------------------------------
'create value lists to be compared
'------------------------------------------------------------------

strARR1 = "5,10,15,20,25,30"
strARR2 = "5,6,7,8,9,10"

Dim ARR1, ARR2

'------------------------------------------------------------------
'these two lines output lists for user to see during comparisons
'------------------------------------------------------------------

response.Write("ARR1= (" & strARR1 & ")<BR>")
response.Write("ARR2= (" & strARR2 & ")<BR>")

'------------------------------------------------------------------
'Convert lists into arrays
'------------------------------------------------------------------

ARR1 = split(strARR1,",")
ARR2 = split(strARR2,",")

'------------------------------------------------------------------
'Compare arrays and find values common to both
'------------------------------------------------------------------

If request("buttonchoice") = "common" then
    strOutput = "("
    for i = 0 to ubound(ARR1)
        for j = 0 to ubound(ARR2)
        if ARR1(i)=ARR2(j) then strOutput=strOutput & ARR1(i) & ","
        next
    next

    strOutput = Left(strOutput, Len(strOutput)-1)
    strOutput = strOutput & ")"

end if

'------------------------------------------------------------------
'Compare arrays and find values uncommon to array 1
'------------------------------------------------------------------

If request("buttonchoice") = "uncommon to 1" then
strOutput = "("
for i = 0 to ubound(ARR2)
    found = "false"
        for j = 0 to ubound(ARR1)
         if (ARR2(i) = ARR1(j)) then
         found = "true"
         exit for
         end if
        next
    if found ="false" then
     strOutput=strOutput & ARR2(i) & ","
     ARR2temp = ARR2(i)
    end if
next

    strOutput = Left(strOutput, Len(strOutput)-1)
    strOutput = strOutput & ")"

end if

'------------------------------------------------------------------
'Compare arrays and find values uncommon to array 2
'------------------------------------------------------------------

If request("buttonchoice") = "uncommon to 2" then
strOutput = "("
for i = 0 to ubound(ARR1)
    found = "false"
        for j = 0 to ubound(ARR2)
         if (ARR1(i) = ARR2(j)) then
         found = "true"
         exit for
         end if
        next
    if found ="false" then
     strOutput=strOutput & ARR1(i) & ","
     ARR1temp = ARR1(i)
    end if
next

    strOutput = Left(strOutput, Len(strOutput)-1)
    strOutput = strOutput & ")"

end if

'------------------------------------------------------------------
'Write output to screen, highlight in red
'------------------------------------------------------------------

If Len(request("buttonchoice")) > 0 then
Response.write( "<BR><FONT color='red'>" & request("buttonchoice") & " = " & strOutput & "</FONT><BR>")
end if

%>

<HTML>
<HEAD>
<TITLE>Comparing Two Arrays</TITLE>
<META name="description" content="">
<META name="keywords" content="">
<META name="generator" content="VisualN++">
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

'------------------------------------------------------------------
'Form to collect user selection
'------------------------------------------------------------------

<Form Name="UserForm" Action="<%=strScriptName%>" Method="Post">

Compare for common:
<INPUT TYPE="radio" NAME="buttonchoice" VALUE="common"><BR>

Compare for uncommon to 1:
<INPUT TYPE="radio" NAME="buttonchoice" VALUE="uncommon to 1"><BR>

Compare for uncommon to 2:
<INPUT TYPE="radio" NAME="buttonchoice" VALUE="uncommon to 2"><BR>

<BR><INPUT TYPE="submit" NAME="submit" VALUE="Submit or Start Over">

</Form>

</BODY>
</HTML>


Enjoy!

Rate this article
Low High
Return To Top
corner image
corner image