具有重复项的ASP Classic 2数组,2个没有重复项的新数组并总结

时间:2016-01-29 13:38:37

标签: arrays asp-classic

我有2个数组,一个有类别,另一个有价格。 2个阵列已连接,因此索引与它们匹配。我希望从中获得2个新阵列,但没有重复类别,但总结了匹配的价格。

Array1 类别()

  1. Öppen
  2. LASK / VATTEN
  3. Bartillbehör
  4. VIN
  5. VIN
  6. 苹果酒
  7. 苹果酒
  8. 苹果酒
  9. 苹果酒
  10. Likör
  11. Likör
  12. 精神公司
  13. Förbrukningsmatrial
  14. Array2 SumSamFattArray()

    1. 600075
    2. 3157,38
    3. 0
    4. 7153
    5. 104
    6. 64
    7. 350
    8. 200
    9. 0
    10. 0
    11. 0
    12. 0
    13. 2643,736
    14. 0
    15. 这是我想要的结果:

      NewFirst Array()

      1. Öppen
      2. LASK / VATTEN
      3. Bartillbehör
      4. Vin(Vin合并)
      5. 苹果酒(苹果酒组合)
      6. Likör(likör合并)
      7. 精神公司
      8. Förbrukningsmatrial
      9. NewSekond数组()

        1. 600075
        2. 3157,38
        3. 0
        4. 7153
        5. 168(vin合并)
        6. 550(苹果酒合并)
        7. 0(Likör合并)
        8. 2643,736
        9. 0
        10. 我一直在努力奋斗2天。感觉就像我尝试了一切。

          如何组合这两个阵列?

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