Click to See Complete Forum and Search --> : two-dimensional array


janetb
12-13-2004, 09:10 AM
I'm trying to grab the values from checkboxes that will always be in threes and put them into a 2-d array in order to loop for insert into sql. I've tried this from an earlier post, but I keep getting a vbscript "Subscript out of range: '[number: 2]' " - the line referenced is the arArray2(iLoop Mod 3,Int(iLoop/3..... (Modified from earlier posting response by Tom.)

contents of request("jid")=1,1,4, 1,1,8, 6,13,13, 5,9,9, 4,22,4

arArray1 = Split(request("jid"),",",-1,1) ' Split text into parts
iNum = UBound(arArray1) + 1 'Find the # of parts

If iNum Mod 3 = 0 Then
ReDim arArray2(1,iNum/3) ' Create 2D array
For iLoop = 0 To iNum - 1
' Place into correct indexes of the 2nd array
arArray2(iLoop Mod 3,Int(iLoop/3)) = arArray1(iLoop)
Next
End If

for x=0 to (iNum/3)-1
response.write "<br>data1=" & arArray2(0,x) & ", data2=" & arArray2(1,x) & ", data3=" & arArray3(2,x)
next

This is not my area of expertise, so any help is appreciated....

Thanks

javaNoobie
12-13-2004, 10:14 PM
I think you were suppose to split by the spaces

Split(Request("jid")," ")

janetb
12-14-2004, 11:23 AM
Nope, that part was fine - it was the 2d array population that was confusing. (Thanks for trying, though) However, got some help and the following works in case anyone else needs it:

arArray1 = Split(str,",",-1,1) ' Split text into parts
iNum = UBound(arArray1) + 1 'Find the # of parts

If iNum Mod 3 = 0 Then
ReDim arArray2(2,iNum/3) ' Create 2D array
For iLoop = 0 To iNum - 1
' Place into correct indexes of the 2nd array
arArray2(iLoop Mod 3,Int(iLoop/3)) = arArray1(iLoop)
Next
End If

for x=0 to (iNum/3)-1
response.write "data1=" & arArray2(0,x) & ", data2=" & arArray2(1,x) & ",
data3=" & arArray2(2,x) & vbnewLine
next

russell
12-15-2004, 12:46 AM
try:

Dim arTemp '' will hold the 2nd dimension array (by splitting the querystring)
Dim arFinal '' this will be out 2D array
Dim i '' just a counter
Dim j '' same here
Dim strTemp '' contents of the request object
Dim u '' upper bound of arTemp

strTemp = "1,1,4, 1,1,8, 6,13,13, 5,9,9, 4,22,4"

arTemp = Split(strTemp, " ") '' split on a space, not an empty string.
u = UBound(arTemp)

ReDim arFinal(2, u) '' Initialze our final array

For i = 0 To u
For j = 0 To 2
'' the split function will do nicely here too,
'' but if there are less than 3 elements we'll get
'' an error!
arFinal(j, i) = Split(arTemp(i), ",")(j)
Next
Next

'' write it out to the web page to show that it works:
For i = 0 To UBound(arFinal, 2)
Response.Write arFinal(0, i) & ", "
Response.Write arFinal(1, i) & ", "
Response.Write arFinal(2, i) & "<br>" & vbCrLf
Next
Aren't multi-dimensional arrays fun?!! :)
** EDIT - Oops, thought you were still working on it. Both ways will work tho... ***