0

I have a 2d array in classic asp like
1-5
1-3
2-5

I need this array output in following format
1-8
2-5

please help me

2
  • Are you looking just to get a sum of all the values in the 1st dimension? Commented Jan 10, 2013 at 11:32
  • yes, I'm looking just to get a sum of all the values in the 1st dimension. Commented Jan 10, 2013 at 11:41

1 Answer 1

2

You need a dictionary to sum up the col2 values grouped by the col1 values. As in:

  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

output:

======
1 8
2 5
======
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's useful for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.