convert column in array to integer value

# Thread: convert column in array to integer value

1. Join Date
Dec 1969
Posts
90

## convert column in array to integer value

I have a multidimension array and all the values in it are strings. I would like to convert 1 column of the array to integer values. How would I go about doing this?

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: convert column in array to integer value

For row = 0 To UBound( yourarray, 2 )<BR> yourarray( theColumn, row ) = CLNG( yourarray( theColumn, row ) )<BR>Next<BR><BR>

3. Join Date
Dec 1969
Posts
90

## RE: convert column in array to integer value

I seem to have missed something- I keep getting the following error: "type mismatch: CLNG" when I run the following:<BR><BR>&#060;%<BR><BR>For row = 0 To UBound( localcart, 2 ) <BR> localcart( cartpquantity, row ) = CLNG( localcart( cartquantity, row ) ) <BR>Next <BR><BR>%&#062;<BR><BR>The array is correct and I ran:<BR> &#060;%<BR>for i=0 to ubound(localcart)<BR>response.write localcart(cartpquantity,i) & "<BR>"<BR>next<BR>%&#062;<BR><BR>to make sure that I had the right column. What am I doing wrong?

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Just what error says...

...at least one of the rows in there has a value in that column that is *not* a legitimate number.<BR><BR>Okay...lets find out which:<BR><BR>&#060;%<BR>For row = 0 To UBound( localcart, 2 ) <BR> temp = localcart( cartquantity, row ) <BR> If IsNumeric( temp ) Then<BR> localcart( cartpquantity, row ) = CLNG( temp )<BR> Else<BR> Response.Write "In row " & row & ", cartpquantity is not numeric: [" & temp & "]&#060;P&#062;"<BR> End If<BR>Next <BR>%&#062;<BR>

5. Join Date
Dec 1969
Posts
90

## Aha

Right on key Bill- the array has a lot of empty values in it. In my poor coding I created a static array with 20 rows in it. During my testing I only had values in rows 1 and 2, so 3-20 I got: "In row 20, cartpquantity is not numeric: []"<BR><BR>I know I should go back and redim my array from the get go, but is there an easy way to get rid of those empty rows when I try to run the CLNG code?

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Why not just ignore...

...the rows where the quantity is non-numeric???<BR><BR>Just remove the ELSE part of that test. If it&#039;s numeric, convert it. If not, leave it alone.<BR><BR>You can even use that as a "flag" to indicate that a given slot in the array is "available". <BR><BR>Aren&#039;t you keeping track of the number of items in the cart in a separate variable, though? If so, you could just process that many rows, instead.<BR><BR>

7. Join Date
Dec 1969
Posts
90

## Yes, ignoring the rows

works well. <BR><BR>New related problem: you gave me code yesterday to convert array to string, I put string in hidden variable, pass to next page, and return string to array. Now I have converted the integers of the original array back to integers. (This works great and all calculations work when I test it with an array that I define on a test page.)<BR><BR>However, since my string has empty values in it in my deployment site, when I run the code to put the string back into an array:<BR><BR>&#060;% <BR>Function StringToArray( str ) <BR> Dim rows, cols, row, col <BR> rows = Split( str, "!" ) <BR> cols = Split( rows(0), "#" ) &#039; this is jut to get number of columns <BR> Dim localcart() <BR> ReDim localcart( UBound(cols), UBound(rows) ) <BR> For row = 0 To UBound(rows) <BR> cols = Split( rows(row), "#" ) <BR> For col = 0 To UBound(cols) <BR> localcart( col, row ) = cols( col ) <BR> Next <BR> Next <BR> StringToArray = localcart <BR>End Function <BR>%&#062; <BR><BR>On page submit, I get the following error:<BR><BR>Subscript out of range: &#039;[number: 0]&#039; <BR><BR>The error points to the following line of code:<BR><BR>cols = Split( rows(0), "#" )<BR><BR>I assume this is due to having empty values in the array. Is there a way to add a IsNull expression to the above code so that when the string is converted back to array, empty values will be ignored and not put in the array?

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ooogg...

That *is* a problem.<BR><BR>&#062; I assume this is due to having empty values in the array. <BR>I don&#039;t see how.<BR><BR>Something is even more drastically wrong.<BR><BR>That says that the line *above* that, did not work!<BR><BR>rows = Split( str, "!" )<BR><BR>Because if it did, then rows *HAS* to be an array, and if it is an array of *any* size, then rows(0) must exist!<BR><BR>Maybe you could dump out the string before doing the split? For debugging?<BR><BR>Function StringToArray( str ) <BR> Dim rows, cols, row, col <BR> Response.Write "DEBUG: cookie string [" & str & "]&#060;HR&#062;" & vbNewLine<BR> rows = Split( str, "!" ) <BR> cols = Split( rows(0), "#" )<BR> ...<BR><BR>What does that show you for the debug output?<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•