This one keeps coming back to hit me in the face over again.

' This is the only way I could get this to work. Although, I will not
know what the values in that split statement are. More importantly the
string in the split statement could be null.

Dim strVal : strVal = "one,two,three"


Dim arrOne
arrOne = split(strVal,",")
Ubound(arrOne) ' returns 2
Response.Write arrOne(1) ' prints two


Dim arrOne()
arrOne = split(strVal,",") <---- Type mismatch


Dim arrOne()
arrOne() = split(strVal,",") <------ Subscript out of range



' Here's another one.

Dim arrOne()
Response.Write Ubound(arrOne) <---- subscript out of range

Dim arrOne()
arrOne = Array() <----- Type mismatch


Dim arrOne()
arrOne() = Array() <-------- Subscript out of range


Dim arrOne
arrOne = Array()
Response.Write Ubound(arrOne) <---- Returns -1
' So why is it that the Only way that works, is the way that i've always
been told not to do it.



And I still have yet to find a function that can properly see if an
array is empty.
Ie: if an array is populated from a recordset using GetRows, and no rows
are returned.

isArray = true
isEmpty = false
varType = 8204
typeName = variant()
Ubound <--- Out of range.

I have no way of knowing if the array is populated or not. I have to
use a For loop on the record set to see if it's populated.. There has to
be a better way.

Re: im sorry.. yet another array question. by mayayana

mayayana
Tue Jun 13 09:11:51 CDT 2006

It *is* a pain.

I think that one thing you have to keep in mind
is that a static array: arrOne
and a dynamic array: arrOne()
are two very different things. One is a variable with
the potential to hold an array, while the other already
is an array.

If you start with a dynamic array you can resize
it at any point, and you start with an unambiguous
array that contains no elements. Declaring: Dim arrOne
has its own purposes but it's ambiguous. It's just a
variant until it's been used with Split or something similar
that fills it with an array.

IsArray should really test whether the array is
actually, really an array with elements, but it only tests for
datatype. It confirms that the variable is of type array,
but of course that's nearly useless information.
You know from the code whether it's an array data
type at the time of the call. What you don't know is whether
it's an array for all practical purposes...whether it has any
elements, so that you can handle it without raising errors.

Personally I use error trapping and planned
array structures. By the latter I mean doing things
like using the first index for information. For example,
if I want to return an array from a function I'll make
sure that it's always an actual array by using ReturnArray(0)
to hold the UBound, or even by just leaving RetrunArray(0)
empty. That way the calling process can do something
like:
A = GetResult(blah)
If UBound(A) > 0 Then 'there's data
OR
If A(0) > 0 Then 'there's data and UBound(A) is stored in 0 index.

In the case of Split, maybe the easiest way
to look at it is that arrOne() has been defined
and structured already. Whether you dim some
elements or not, arrOne() already points to
an array structure. An array is a variant but a
variant is not necessarily an array. So with
arrOne() you're not giving Split a variable that it
can use to store the return value.

Well, I've gone on for awhile here. Maybe someone
else will have a more concise explanation. :)

> This one keeps coming back to hit me in the face over again.
>
> ' This is the only way I could get this to work. Although, I will not
> know what the values in that split statement are. More importantly the
> string in the split statement could be null.
>
> Dim strVal : strVal = "one,two,three"
>
>
> Dim arrOne
> arrOne = split(strVal,",")
> Ubound(arrOne) ' returns 2
> Response.Write arrOne(1) ' prints two
>
>
> Dim arrOne()
> arrOne = split(strVal,",") <---- Type mismatch
>
>
> Dim arrOne()
> arrOne() = split(strVal,",") <------ Subscript out of range
>
>
>
> ' Here's another one.
>
> Dim arrOne()
> Response.Write Ubound(arrOne) <---- subscript out of range
>
> Dim arrOne()
> arrOne = Array() <----- Type mismatch
>
>
> Dim arrOne()
> arrOne() = Array() <-------- Subscript out of range
>
>
> Dim arrOne
> arrOne = Array()
> Response.Write Ubound(arrOne) <---- Returns -1
> ' So why is it that the Only way that works, is the way that i've always
> been told not to do it.
>
>
>
> And I still have yet to find a function that can properly see if an
> array is empty.
> Ie: if an array is populated from a recordset using GetRows, and no rows
> are returned.
>
> isArray = true
> isEmpty = false
> varType = 8204
> typeName = variant()
> Ubound <--- Out of range.
>
> I have no way of knowing if the array is populated or not. I have to
> use a For loop on the record set to see if it's populated.. There has to
> be a better way.
>
>
>
>
>
>
>



Re: im sorry.. yet another array question. by Eric

Eric
Tue Jun 13 09:30:26 CDT 2006

mayayana wrote:
> It *is* a pain.
Glad to see im not alone !
>
> I think that one thing you have to keep in mind
> is that a static array: arrOne
> and a dynamic array: arrOne()
> are two very different things. One is a variable with
> the potential to hold an array, while the other already
> is an array.
I have tried to to Dim arrOne(). When you use GetRows, you cannot put
brackets on your dim statement. I've tried numerous times, but getrows
only works when you leave the brackets off.

>
> If you start with a dynamic array you can resize
> it at any point, and you start with an unambiguous
> array that contains no elements. Declaring: Dim arrOne
> has its own purposes but it's ambiguous. It's just a
> variant until it's been used with Split or something similar
> that fills it with an array.
>
> IsArray should really test whether the array is
> actually, really an array with elements, but it only tests for
> datatype. It confirms that the variable is of type array,
> but of course that's nearly useless information.
> You know from the code whether it's an array data
> type at the time of the call. What you don't know is whether
> it's an array for all practical purposes...whether it has any
> elements, so that you can handle it without raising errors.

I wouldn't say it is useless. But I also wouldn't say we need 3
functions (isArray,varType,typeName) to tell us either. I would say to
keep varType, and toss the other two out of the language. It is kinda
funny that they never made a function for the exact problem that im
running into. I've run into it at least 20 times in the same project!

>
> Personally I use error trapping and planned
> array structures. By the latter I mean doing things
> like using the first index for information. For example,
> if I want to return an array from a function I'll make
> sure that it's always an actual array by using ReturnArray(0)
> to hold the UBound, or even by just leaving RetrunArray(0)
> empty. That way the calling process can do something
> like:
> A = GetResult(blah)
> If UBound(A) > 0 Then 'there's data
> OR
> If A(0) > 0 Then 'there's data and UBound(A) is stored in 0 index.

Yeah, i was thinking that I may have to fill the fist element with
something that shouldn't be there, just to prevent ubound from breaking.
Although i would really rather not do that.


How would i do error trapping for this? The only two ways i know how to
catch an exception is to "resume next", or i think to go specificly to a
error function. Is there a way to just put a line of code. Some thing
that will only be on this function, and not be active for the whole
program?

>
> In the case of Split, maybe the easiest way
> to look at it is that arrOne() has been defined
> and structured already. Whether you dim some
> elements or not, arrOne() already points to
> an array structure. An array is a variant but a
> variant is not necessarily an array. So with
> arrOne() you're not giving Split a variable that it
> can use to store the return value.
>
> Well, I've gone on for awhile here. Maybe someone
> else will have a more concise explanation. :)

Thank you for that!
>
>
>>This one keeps coming back to hit me in the face over again.
>>
>>' This is the only way I could get this to work. Although, I will not
>>know what the values in that split statement are. More importantly the
>>string in the split statement could be null.
>>
>>Dim strVal : strVal = "one,two,three"
>>
>>
>>Dim arrOne
>>arrOne = split(strVal,",")
>>Ubound(arrOne) ' returns 2
>>Response.Write arrOne(1) ' prints two
>>
>>
>>Dim arrOne()
>>arrOne = split(strVal,",") <---- Type mismatch
>>
>>
>>Dim arrOne()
>>arrOne() = split(strVal,",") <------ Subscript out of range
>>
>>
>>
>>' Here's another one.
>>
>>Dim arrOne()
>>Response.Write Ubound(arrOne) <---- subscript out of range
>>
>>Dim arrOne()
>>arrOne = Array() <----- Type mismatch
>>
>>
>>Dim arrOne()
>>arrOne() = Array() <-------- Subscript out of range
>>
>>
>>Dim arrOne
>>arrOne = Array()
>>Response.Write Ubound(arrOne) <---- Returns -1
>>' So why is it that the Only way that works, is the way that i've always
>>been told not to do it.
>>
>>
>>
>>And I still have yet to find a function that can properly see if an
>>array is empty.
>>Ie: if an array is populated from a recordset using GetRows, and no rows
>>are returned.
>>
>>isArray = true
>>isEmpty = false
>>varType = 8204
>>typeName = variant()
>>Ubound <--- Out of range.
>>
>>I have no way of knowing if the array is populated or not. I have to
>>use a For loop on the record set to see if it's populated.. There has to
>>be a better way.
>>
>>
>>
>>
>>
>>
>>
>
>
>

Re: im sorry.. yet another array question. by Bob

Bob
Tue Jun 13 09:28:43 CDT 2006

Nice job. Just one quibble (see below):
mayayana wrote:
> It *is* a pain.
>
> I think that one thing you have to keep in mind
> is that a static array: arrOne
> and a dynamic array: arrOne()
> are two very different things. One is a variable with
> the potential to hold an array, while the other already
> is an array.

dim arrOne 'empty variant variable
dim arrOne(1) 'static array - cannot be redimmed
dim arrOne() 'dynamic array



--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: im sorry.. yet another array question. by Eric

Eric
Tue Jun 13 10:05:41 CDT 2006

Bob Barrows [MVP] wrote:
> Nice job. Just one quibble (see below):
> mayayana wrote:
>
>>It *is* a pain.
>>
>> I think that one thing you have to keep in mind
>>is that a static array: arrOne
>>and a dynamic array: arrOne()
>>are two very different things. One is a variable with
>>the potential to hold an array, while the other already
>>is an array.
>
>
> dim arrOne 'empty variant variable
> dim arrOne(1) 'static array - cannot be redimmed
> dim arrOne() 'dynamic array
>
>
>

Although some functions will only work on a variant.
But for this problem, I need to change the cast altogher. It's as if any
built in array functions are useless.

All i want to do is a split, and populate an array. If the split didn't
have any values, then it should make an array that doesn't ahve any values.

What i need to do now is this, and im really hoping there is a better way:

function mySplit (strVals, strDelim)
' count how many times strDelim is in strVals.

if count > 0 then
Dim arrSplit
split....
mySplit = arrSplit
exit function
Else
cbool(arrSplit)
arrSplit = False
exit function
end if
End Function

Re: im sorry.. yet another array question. by Justin

Justin
Tue Jun 13 10:54:26 CDT 2006

On Tue, 13 Jun 2006 10:05:41 -0500, Eric <nomail@domain.tld> wrote:

> All i want to do is a split, and populate an array. If the split didn't
> have any values, then it should make an array that doesn't ahve any
> values.

Try this:

Function mySplit(strVals, strDelim)
If InStr(strVals, strDelim) > 0 Then
mySplit = Split(strVals, strDelim)
Else
mySplit = Array()
End If
End Function


Though I do suggest you choose a more descriptive name than "mySplit" in
your production code. :)


--
Justin Piper
Bizco Technologies
http://www.bizco.com/

Re: im sorry.. yet another array question. by Bob

Bob
Tue Jun 13 10:59:40 CDT 2006

Eric wrote:
> Bob Barrows [MVP] wrote:
>> dim arrOne 'empty variant variable
>> dim arrOne(1) 'static array - cannot be redimmed
>> dim arrOne() 'dynamic array
>>
> Although some functions will only work on a variant.
> But for this problem, I need to change the cast altogher. It's as if
> any built in array functions are useless.
>
> All i want to do is a split, and populate an array.

What the split() method does is take a string, generate an array behind
the scenes containing elements from the string, and assign the resulting
array to the variant variable on the left side of the "=" sign.

IOW, you don't start with an array and expect split() to populate it.
You start with a variant variable which gets assigned the result of the
split() method. This applies to GetRows() as well.

> If the split
> didn't have any values, then it should make an array that doesn't
> ahve any values.

There should always be at least one element in the resulting array,
provided that the supplied string's length was > 0.

dim s, delim
dim ar
s="a"
delim=","
ar=split(s,delim)
msgbox ubound(ar)


>
> What i need to do now is this, and im really hoping there is a better
> way:
>
> function mySplit (strVals, strDelim)
> ' count how many times strDelim is in strVals.

Well, here is one way, assuming that strDelim is a single character:

strVals= "a,b,c,d"
strDelim = ","
countDelims=len(strVals) - len(replace(strVals,strDelim,"")
countElements = countDelims + 1

>
> if count > 0 then
> Dim arrSplit
> split....
> mySplit = arrSplit
> exit function
> Else
> cbool(arrSplit)

You need to assign the result of cbool to a variable ... even the same
variable:

arrsplit=cbool(arrSplit)

> arrSplit = False

Actually, arrSplit was already declared as a variant ... you did not
even need the cbool() function.
Also, at this point, you should probably have set mySplit directly to
false, bypassing the previous lines entirely

> exit function
> end if
> End Function

Well, here is one way, assuming that strDelim is a single character:

strVals= "a,b,c,d"
strDelim = ","
countDelims=len(strVals) - len(replace(strVals,strDelim,"")
countElements = countDelims + 1




--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: im sorry.. yet another array question. by Eric

Eric
Tue Jun 13 11:57:47 CDT 2006


> There should always be at least one element in the resulting array,
> provided that the supplied string's length was > 0.

Right, the problem is when the supplied strings length is = 0, or the
string is null.
Ie: my CSV list is empty, or the GetRows SELECT returns no rows.

>
> dim s, delim
> dim ar
> s="a"
> delim=","
> ar=split(s,delim)
> msgbox ubound(ar)
>
>
>
>>What i need to do now is this, and im really hoping there is a better
>>way:
>>
>>function mySplit (strVals, strDelim)
>> ' count how many times strDelim is in strVals.
>
>
> Well, here is one way, assuming that strDelim is a single character:
>
> strVals= "a,b,c,d"
> strDelim = ","
> countDelims=len(strVals) - len(replace(strVals,strDelim,"")
> countElements = countDelims + 1
>
>
>> if count > 0 then
>> Dim arrSplit
>> split....
>> mySplit = arrSplit
>> exit function
>> Else
>> cbool(arrSplit)
>
>
> You need to assign the result of cbool to a variable ... even the same
> variable:
>
> arrsplit=cbool(arrSplit)
>
>
>> arrSplit = False
>
>
> Actually, arrSplit was already declared as a variant ... you did not
> even need the cbool() function.
> Also, at this point, you should probably have set mySplit directly to
> false, bypassing the previous lines entirely
>
>
>> exit function
>> end if
>>End Function
>
>
> Well, here is one way, assuming that strDelim is a single character:
>
> strVals= "a,b,c,d"
> strDelim = ","
> countDelims=len(strVals) - len(replace(strVals,strDelim,"")
> countElements = countDelims + 1
>
>
>
>

And of course the hope was to not have to write a wrapper around
functions like this... oh well .. :(

Re: im sorry.. yet another array question. by Eric

Eric
Tue Jun 13 12:07:18 CDT 2006

I also tried it the perl way, but it didn't work

aryTwo = split(aryOne(1,0),",") or False


aryTwo = split(aryOne(1,0),",") If instr(aryOne(1,0),",")


if instr(aryOne(1,0),",") then aryTwo = split(aryOne(1,0),",") end if


no beans... i'll write a wrapper