Split Function Help

liunx

Guest
I am trying to use the Split function to split an array passed as hidden elements in a form (which has happened successfully) and I keep getting errors. I have never used this function before and I have tried to pattern it after a tutorial but to no avaial.

Here is what the tutorial used:

<%
Dim strPaco
strPaco = "This is Paco's String"

arrPaco = Split(strPaco," ")

For i = 0 to 3
Response.Write arrPaco & "<br">
Next
%>

This is what just took place.
We took our string and split it everywhere there was a space and put it in an array. We know there are 4 words so we used 3 in our loop (arrays are ZERO based, 0 1 2 3. Arrays are explained in-depth here).

Now this script will write:
This
is
Paco's
String


I notice that they did not dim arrPaco-is that an error? Also, do you have to know the number of elements that the array will contain or can you redim preserve your way through it?

Here is what I have tried to use and I keep getting type mismatches on the response.write line:

Dim strString, arrString(1)

strString = "This is Bob's, string"
For i = 0 to 1
arrString(i) = Split(strString, ",")
Response.Write (arrString(i) & "<br />")
Next


Thanks!You are doing it slightly wrong. When you do a split, it creates the array for you. Also, your tutorial is failing to make use of some of the array tools.

This is how I would do your code:


Dim strString, arrString()

strString = "This is Bob's, string"
arrString = Split(strString, ",")

For i = 0 to UBound(arrString) - 1
Response.Write (arrString(i) & "<br />")
Next


UBound will get the highest array value or the 'upper bound'. The reason I subtract 1 is because of the nature of arrays. The LBound (lower bound) of an array is pretty much always 0. But if you get the UBound in your case it will return the number 2 because there are 2 elements in the array. Since LBound starts at 0, you will be using 0 and 1 from the array.

If I was to not subtract 1, the loop would assume there was 3 elements and once it got to arrString(2) is would throw and index out of range error.Actually......

Ryan's .NET days must be wearing on his ClASP (Classic ASP) skills :P

You don't need the -1 on the ubound unless there was a trailing , on the string like "Bob's house,string,"

Also, the () on the end of the array is not necessary in ClASP.putts is correct. I haven't used C|ASP in a while as I have moved on to better pastures.Originally posted by Bobba Buoy
I am trying to use the Split function to split an array passed as hidden elements in a form (which has happened successfully) and I keep getting errors. I have never used this function before and I have tried to pattern it after a tutorial but to no avaial.

Here is what the tutorial used:

<%
Dim strPaco
strPaco = "This is Paco's String"

arrPaco = Split(strPaco," ")

For i = 0 to 3
Response.Write arrPaco & "<br">
Next
%>

This is what just took place.
We took our string and split it everywhere there was a space and put it in an array. We know there are 4 words so we used 3 in our loop (arrays are ZERO based, 0 1 2 3. Arrays are explained in-depth here).

Now this script will write:
This
is
Paco's
String


I notice that they did not dim arrPaco-is that an error? Also, do you have to know the number of elements that the array will contain or can you redim preserve your way through it?

Here is what I have tried to use and I keep getting type mismatches on the response.write line:

Dim strString, arrString(1)

strString = "This is Bob's, string"
For i = 0 to 1
arrString(i) = Split(strString, ",")
Response.Write (arrString(i) & "<br />")
Next


Thanks!
Its not an error not to dim something unless you specify "Option Explicit" on the top of the page. The only thing it does it tell the runtime script engine that the pages variables are declared and not to expect anything that is just created with out dim statement.

The type mismatch occurs because you're creating another array and placing it inside an array but accessing it like its a string. it would be something like

for j = 0 to Ubound(arrString(i))
Call Response.Write(arrString(i)(j))
NextVery helpful! Thanks!!
 
Back
Top