clum1
clum1

Reputation: 23

Classic ASP - combining two lists/arrays

I'm using a bit of js to repeat extra pairs of dropdowns in a form i.e.

Item 1: description - size
Item 2: description - size
Item 3: description - size

As each dropdown has the same form field name, this gives me two comma separated outputs, e.g.

1, 2, 7 and 2, 5, 6

As I write these to my database, I need to combine the two lists to become:

(1, 2), (2, 5), (7, 6)

to build a repeating SQL statement. How do I achieve this? I've tried all sorts of FOR loops but just can't seem to crack it. Is there an obvious function I'm missing?

Code:

UniformItemArray=split(UniformItem,",")
UniformSizeArray=split(UniformSize,",")

iUniform=0
iSize=0
FOR EACH Uniform in UniformItemArray
  Response.Write("("&trim(Uniform)&",")

  FOR EACH Size in UniformSizeArray
    iSize=iUniform
    IF cint(iUniform)=cint(iSize) THEN
      Response.Write(Size)
    END IF
  NEXT
  Response.Write(") - ")
    iUniform=iUniform+1
  NEXT

This is returning:

(1,2 5 6) - (2,2 5 6) - (7,2 5 6) - 

What I need is

(1,2) - (2,5) - (7,6) - 

I've added brackets and dashes to clarify. Basically, it's looping correctly through the first array, but despite addition of IF statements and various other permutations to the second loop, I can't get it to show the correct value for the second array. Once I can get this code correct, I'll use it to build the SQL statement.

Upvotes: 2

Views: 1195

Answers (1)

user692942
user692942

Reputation: 16682

If your two arrays are always in sync (order of the elements in both arrays match) then this is quite simple. Instead of a For Each loop just use a standard For loop and make Uniform your counter.

Dim UniformItemArray, UniformSizeArray
Dim Uniform, Uniforms

UniformItemArray = Split(UniformItem, ",")
UniformSizeArray = Split(UniformSize, ",")

If IsArray(UniformItemArray) Then
  Uniforms = UBound(UniformItemArray)
  For Uniform = 0 To Uniforms
    'Add a comma after the first iteration
    If Uniform > 0 Then Response.Write ","
    Response.Write "(" & UniformItemArray(Uniform) & "," & UniformSizeArray(Uniform) & ")"
  Next
End If

Upvotes: 1

Related Questions