Two sets of string data need to compare the same data and add their values to form a new string data a1=sp2=20;sp1=34;
a2=sp3=2;sp2=3;sp1=4;
Two sets of string data. Add the same data values in the string to get a new set of data.
That is, sp3=2;sp2=23;sp1=38
(ps A simple application: the original quantity of product 2 is 20 pieces, the original quantity of product 1 is 34 pieces, newly purchased or newly sold 3 pieces of product 2, 4 pieces of product 1, etc. are simulated to calculate the purchase volume and sales volume and inventory, a small purchase, sales and inventory system can use this method)
So how to compare and merge two sets of string data with the same data?
First, connect and combine the two sets of string data
a3=a1&a2
Then a3=sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;
Second, add the same data in a3
The main solution here is how to find the same data
First of all, because now a3 is composed of sp2, sp1, sp3, sp2 and sp1, it is necessary to find the same sp2 and sp1 separately and then add the values.
Split by split function; get each piece of data and value for delimiter.
That is, s_array = split(a3,;) through for i = 0 to ubound(s_array) loop we can get individual data and values
The format of each item is similar to sp2=20. Sp2 needs to be extracted to compare it with the data in the same group, so an independent function is needed to extract it.
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.
Secondly, each piece of data is decomposed, that is, how to find the same data name.
We assume such a process, first extract the first element in the a3 array, compare it with the data before dividing the first element, and add it if they are 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 have obtained the final value and can assign the value to a new dynamic array at any time to form the final combined data array.
redim Preserve result(p)
result(p) = getSPName(s_array(i)) & = & Nums
Right now
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 like this: when a subsequent element in the a3 array is always operated on with the same element compared before, so this element cannot be included in for i = 0 to ubound(s_array ) within result(p) = getSPName(s_array(i)) & = & Nums dynamic array.
How to solve the problem of no longer comparing elements that have already been compared
We must mark the elements that have been compared. For example, in the a3 array (a3=sp2=20;sp1=34;sp3=2;sp2=3;sp1=4;) after taking out sp2=20, the comparison operation will end A sp2=3, at this time, the array element number of sp2=3 will be marked after the comparison operation, and this element will not be counted in the next loop comparison.
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
The definition of ID(q)=j is to mark the element that is currently the same and assign it to the dynamic array id(q). q is defined as 0 by default, and q=q+1 is looped again.
Then with this mark, we can selectively choose to compare and accumulate.
define function
functionIsInID(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 in result
mainhb=mainhb&x&;
next
end function
The overall function is
<%
dim result()
dimID()
dim p , q , Nums
p=0
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 in result
mainhb=mainhb&x&;
next
end function
Function getSPName(sp)
getSPName = split(sp,=)(0)
End Function
Function getSPNum(sp)
getSPNum = split(sp,=)(1)
end function
functionIsInID(j)
dim x
IsInID = false
for each x in ID
if x = j then
IsInID = true
exit function
End if
Next
end function
%>