在经典asp中的多维数组中添加重复值

时间:2013-01-10 11:04:17

标签: asp-classic vbscript

我在经典的asp中有一个二维数组
1-5
1-3
2-5

我需要以下格式输出这个数组 1-8
2-5

请帮帮我

1 个答案:

答案 0 :(得分:2)

您需要一本字典来总结按col1值分组的col2值。如:

  ReDim aIn(2, 1)
  aIn(0, 0) = 1 : aIn(0, 1) = 5
  aIn(1, 0) = 1 : aIn(1, 1) = 3
  aIn(2, 0) = 2 : aIn(2, 1) = 5
  Dim dicX : Set dicX = CreateObject("Scripting.Dictionary")
  Dim i
  For i = LBound(aIn, 1) To UBound(aIn, 1)
      dicX(aIn(i, 0)) = dicX(aIn(i, 0)) + aIn(i, 1)
  Next
  ReDim aOut(dicX.Count - 1, 1)
  For i = LBound(aOut, 1) To UBound(aOut, 1)
      aOut(i, 0) = dicX.Keys()(i)
      aOut(i, 1) = dicX(aOut(i, 0))
  Next
  For i = LBound(aOut, 1) To UBound(aOut, 1)
      WScript.Echo aOut(i, 0), aOut(i, 1)
  Next

输出:

======
1 8
2 5
======