ASP two sets of string data comparison merge same data _ Application tips

Source: Internet
Author: User
Tags new set
A1= "SP2=20;SP1=34;"
A2= "SP3=2;SP2=3;SP1=4;"
Two sets of string data, adding the same data values in a string to get a new set of data
namely "sp3=2;sp2=23;sp1=38"

(P.s A simple application: Two of the original number of 20 pieces, the original number of goods 34, new stock or a new sale of 23 products, 14 items such as the type of simulation to calculate the purchase volume, sales and inventory, small invoicing system can use such a method)

So how do you implement two sets of string data comparisons merging the same data?

First, concatenate the two sets of string data together

A3=a1&a2
So a3= "sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;"

Second, the same data in the A3 is added to the value

The main solution here is how to find the same data

First, because now A3 is made up of SP2, SP1, SP3, SP2 and SP1, the same SP2 and SP1 need to be identified separately to be added.

Split ";" by split function. Gets each piece of data and value for the separator.
i.e. S_array = Split (A3, ";") With the For i = 0 To UBound (S_array) loop we get separate data and values

The format of each item is similar to "sp2=20", to be extracted from the SP2 can be compared with the data in the same group, so also need independent function to extract

Function Getspname (SP)
Getspname = Split (sp, "=") (0)
End Function

Function Getspnum (SP)
Getspnum = Split (sp, "=") (1)
End Function

Get the data name before "=" and the data value after "=" respectively.

Second, each piece of data is decomposed, that is, how to find the same data name.
We assume that such a process first extracts the first element in the A3 array, comparing it to the data that was thought before the first element, and then adding it if the same.


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
Next
Next



We get the final value that can be assigned to a new dynamic array at any time, combined into the final "combined data" array
ReDim Preserve Result (p)
Result (P) = Getspname (S_array (i)) & "=" & Nums
That


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
Next

ReDim Preserve Result (p)
Result (P) = Getspname (S_array (i)) & "=" & Nums
P=p+1
Next




This is bound to encounter a situation in which an element that follows in the A3 array is always calculated with the same element that was previously compared, so that element cannot be counted as result (p) = Getspname in the For i = 0 To UBound (s_array) (S_ Array (i)) & "=" & nums dynamic array.

How to resolve elements that are no longer operational compared to those that have been compared

We must mark the elements that have been compared, such as the A3 array (a3= "sp2=20;sp1=34;sp3=2;sp2=3;sp1=4"). After the sp2=20 is removed, the comparison is performed to the latter sp2=3, at which point the sp2=3 array element number is marked and counted when the next cycle is compared.


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



Where the definition ID (q) =j is the current comparison of the element tag, and assigned to the dynamic array ID (q), q by default defined as 0, again Loop q=q+1
So powerful the tag, we can have a selective selection of the more cumulative.
Defining functions


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




The main function is


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.
mainhb=mainhb&x& ";"
Next
End Function




The whole function is


<%
Dim result ()
Dim ID ()
Dim p, Q, Nums

p =
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.
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
%>

<script language= "vbs" > Dim result (), ID (), p, q, nums p = q= 0 nums = 0 ReDim Preserve ID (q) ID (q) = "" A1 = "a=2;b=4;d=2;" A2= "c=1;a=6;e=5;b=2;" S=A1&AMP;A2 s = Left (S,len (s)-1) alert 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_a Rray) 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 (PS) = 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) GetS Pnum = Split (sp, "=") (1) End Function function Isinid(j) Dim x Isinid = False for each X/ID if x = J Then Isinid = True Exit Function end If Next End Function </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.