Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get individual ints from an array

Status
Not open for further replies.

th3maw

Programmer
Feb 16, 2005
26
GB
I have split a string pulled from a table to into an array made up of numbers. The original string is made up of a list of costs associated with a quote and the number of these can be totally random.

Once split I want to convert the individual numbers in the array to int in order to query another table.

I can loops through and display the data with no problem using UBound, however I am not sure of the most efficient way to isolate individual parts of the array in order to convert them to integers.

Anybody got any tips to point me in the right direction?
 
Use CInt() to convert the array values into Integers. Use CDbl() if any of the numbers are not integers (3.45 for example)
Code:
<%
strText = "32,5,43,56,8,2,5"

strArray = Split(strText,",")

For i=0 To UBound(strArray)
  Response.Write [red]CInt([/red]strArray(i)[red])[/red] & "<BR>"
Next
%>

Tony
_______________________________________________________________
 
Thanks for your response Tony.

I should have pasted the code as the example you have given above is almost exactly what I couldnt get to work. Looking at your example I have been able to figure out where I am going wrong.....


It all previously worked fine till I tried convert to int - got a list of numbers displaying as expected - however whenever I tried to covert to int I kept getting a Type Mismatch error.

The only difference between your code and mine is that where you used -

strText = "32,5,43,56,8,2,5"

I used

strText = "ID145ID136ID162ID163"

or

strText = ",32,5,43,56,8,2,5"


So I have now tried it without the initial ID or , and it works!!

It seems that even though I removed the ID - arrTest = Split(strText,"ID") - and used trim to eliminate any empty spaces the original format was somehow still messing up the code.

As a test if I remove the ID using - StrTest = Right(myArr,18) - It all works perfectly

Very annoying as I have been trying to get round this error for hours... and all I had to do was change the format of the string slightly :/

Still I am glad I finally figured it out as it was driving me crazy. Still unsure why this caused this error, as I am essentially using the same numbers, but now I am getting to them in a different order.

Thanks again for your help Tony ... I find it too easy to miss soemthing simple especially when it makes no sense to me. Running comparitive tests with your code really heped me pin it down.
 
The reason you got the Type Mismatch error when you used strText = "ID145ID136ID162ID163" is because the first entry in your array would have been an empty string since there is nothing in front of the first ID in your string.

Trying to do a CInt() on an empty string will result in that error


Tony
_______________________________________________________________
 

Yeah that does make sense :)

Based on that have now got round the problem without having to make any changes to the data using -

For i = 1 to UBound(arrTest)

Thanks again for your help.
 
>>I want to convert the individual numbers in the array to int in order to query another table

FesterSXS:
since he is to query another database with the values of the array is a cint() necssary(it also has some numbers limit)...

Known is handfull, Unknown is worldfull
 
not actually sure to be honest. I always force any numeric Form/QueryString/Array value to an Int before I use it to compare with a number field in a database. I know it is necessary for the Form/QueryString values - don't know for sure about the Array values.

Tony
_______________________________________________________________
 
If you don't do a Cint then you're using a floating point number, and therefore liable to get rounding errors. You should always used Fixed Point format for any comparisons

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Err...I disagree. If you don't convert from a string to a number in the first place then there is no reason for VBScript to magically convert your array of string variants into numbers. When you concat them into an SQL statement you will be concatting them in as string (if you previous converted to int then they will be cast to string as part of the concat operation). The SQL String itself could care less if you stored them as strings, ints, or MyMagicClass (provided it has a default that returns something VBScript recognizes as a non-array type).
The type of value doesn't matter until it hits the database, but that is based on 1) quotes or lack and 2) if it is possible to cast to the type it is asking for with violating quotes/etc.

IN ASP a string is a string is a string. It doesn't matter if that string will ultimately be sent to the database or to the Response.Buffer, once you add a number into the string it is just another string as far as ASP/VBScript is concerned.

Easiest in VBScript example I can think of is just two strings:
Code:
Dim a, b

a = "SELECT * FROM Table WHERE x = " & "5"
b = "SELECT * FROM Table WHERE x = " & 5

Thge only difference between those two strings is that one requires an additional Cast step internally while concatenating. They both look exactly the same when they are done and would be executed exactly the same by a database.

-T

barcode_1.gif
 
Agree Tarwn that it's better to keep in strings if you can - but if you do convert to numbers then my comment stands

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top