我有2个数组,一个有类别,另一个有价格。 2个阵列已连接,因此索引与它们匹配。我希望从中获得2个新阵列,但没有重复类别,但总结了匹配的价格。
Array1 类别()
Array2 SumSamFattArray()
这是我想要的结果:
NewFirst Array()
NewSekond数组()
我一直在努力奋斗2天。感觉就像我尝试了一切。
如何组合这两个阵列?
答案 0 :(得分:0)
如果你的原始数组按照你的例子中的类别进行排序,那么它非常简单:循环显示价格,随时随地计算,每次类别更改时移动到下一行。 (请注意,为简单起见,我使用了2个二维数组而不是4个单维数组。)
dim PriceList(1,20) '- actually, I imagine these probably come from a database, in which
'- case you'd probably use GetRows rather than explicitly dimming as an array
dim Totals(1,20)
dim n, i
n = 0
Totals(0,0) = ""
For i = 0 to Ubound(Pricelist,2)
If Pricelist(0,i) & "" <> "" Then
If Pricelist(0,i) <> Totals(0,n) Then '- next category
n = n + 1
Totals(0,n) = Pricelist(0,i) '- write up category
Totals(1,n) = 0 '-initialize total
End If
Totals(1,n) = Totals(1,n) + Pricelist(1,i)
End If
Next
如果您的原始价目表不按类别排序,那么您不必在每次类别更改时简单地增加行,而是需要遍历总计数组以找到合适的行如果存在,或者如果它不存在则添加。
dim PriceList(1,20)
dim Totals(1,20)
dim n, i
dim j, maxN
n = 0 : maxN = 0
Totals(0,0) = ""
For i = 0 to Ubound(Pricelist,2)
If Pricelist(0,i) & "" <> "" Then
If Pricelist(0,i) <> Totals(0,n) Then '- not the same category as previous row
n = 0
For j = 0 to maxN
If Pricelist(0,i) = Totals(0,j) Then '- found the category
n = j
Exit For
End If
Next
If n = 0 then '- didn't find the category, so add it to the end of the list
maxN = maxN + 1
n = maxN
Totals(0,n) = Pricelist(0,i)
Totals(1,n) = 0
End If
End If
Totals(1,n) = Totals(1,n) + Pricelist(1,i)
End If
Next
答案 1 :(得分:0)
Dim NyBeskArray()`
Dim NySummaArray()
X=0
Z=0
Y=0
For Each item In SamFattBesk
Rubrik = SamFattBesk(X)
Tal = SumSamFattArray(X)
Tal =Ccur(Tal)
IF NOT RubrikOld = Rubrik THEN
ReDim preserve NyBeskArray(Z)
NyBeskArray(Z) = Rubrik
ReDim preserve NySummakArray(Z)
NySummakArray(Z) = Tal
Z = Z +1
ELSE
Y=X-1
SummaTal = Tal + TalOld
SummaTal = CCur(SummaTal)
ReDim preserve NySummakArray(Y)
NySummakArray(Y) = SummaTal
END IF
X=X+1
RubrikOld = Rubrik
TalOld = Tal
Next
%>