【问题标题】:ASP Classic 2 arrays with duplicates, 2 new arrays with no duplicates and summed upASP Classic 2 个重复的数组,2 个没有重复的新数组并总结
【发布时间】:2016-01-29 13:38:37
【问题描述】:

我有 2 个数组,一个包含类别,一个包含价格。这 2 个数组是 connected 的,因此索引与它们匹配。我想从中获得 2 个新数组,该数组在类别上没有重复,但汇总了匹配的价格。

Array1 Category()

  1. 奥本
  2. 拉斯克/瓦滕
  3. Bartillbehör
  4. Öl
  5. 苹果酒
  6. 苹果酒
  7. 苹果酒
  8. 苹果酒
  9. Likör
  10. Likör
  11. 精神
  12. Förbrukningsmatrial

Array2 SumSamFattArray()

  1. 600,075
  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

这是我想要的结果:

NewFirst 数组 ()

  1. 奥本
  2. 拉斯克/瓦滕
  3. Bartillbehör
  4. Öl
  5. Vin(Vin 组合)
  6. 苹果酒(混合苹果酒)
  7. Likör(likör 组合)
  8. 精神
  9. Förbrukningsmatrial

NewSekond 数组 ()

  1. 600,075
  2. 3157,38
  3. 0
  4. 7153
  5. 168(vin组合)
  6. 550(加苹果酒)
  7. 0(Likör 组合)
  8. 2643,736
  9. 0

我已经为此苦苦挣扎了 2 天。感觉什么都试过了。

如何组合这两个数组?

【问题讨论】:

  • 请向我们展示您的尝试。在您的帖子中包含您的代码。
  • 可以使用一个多维数组,为什么还要使用两个一维数组?
  • @Martha:可能取决于他如何获取提供的数据。

标签: arrays asp-classic


【解决方案1】:

如果您的原始数组像您的示例中那样按类别排序,那么它非常简单:遍历价格,随时合计,每次类别更改时移动到下一行。 (请注意,为简单起见,我使用的是 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

【讨论】:

    【解决方案2】:
    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
        %>
    

    【讨论】:

    • 过了一会儿,我弄清楚了这个页面是如何工作的。这就是我解决它的方法。也许不是最好的方法。坚果它有效。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多