Re: Declaring Arrays for use with Split() by ekkehard
ekkehard
Wed Sep 05 00:09:47 PDT 2007
Al Dunbar schrieb:
> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
> news:46dc2266$0$30371$9b4e6d93@newsspool4.arcor-online.net...
>> dch3 schrieb:
>>> I'm very much in the habit of Dim-ing all of my variables and would like
>>> be able to explicitly Dim an array for use with Split(), however its
>>> giving me a 'Type Mismatch' error when I try to Dim it.
>>>
>>> Dim prefArray(2)
>> This declares/dims an *array variable* with a fixed capacity of 3
>> elements
>>> prefArray = split(objFile.ReadLine, chr(9))
>> The Split() function - as the Array() function - returns a *variant
>> variable*
>> holding an array. From the chapter Array() in the VBScript Docs:
>>
>> A variable that is not declared as an array can still contain an array.
>> Although a Variant variable containing an array is conceptually
>> different
>> from an array variable containing Variant elements, the array elements
>> are accessed in the same way.
>>
>>> The line being read will only, ever have one tab return and thus the line
>>> will only be broken into two values.
>> So:
>>
>> Dim prefArray ' no ()
>> prefArray = Split( objFile.ReadLine, vbTab, 2 )
After reading Al's contribution, I wish I'd written
Dim aTmp : aTmp = Split( sLine, vbTab )
Dim prefArray( 1 ) ' prefArray( 2 )
If 1 = UBound( aTmp ) Then
prefArray( 0 ) = aTmp( 0 ) ' prefArray( 1 ) = aTmp( 0 )
prefArray( 1 ) = aTmp( 1 ) ' prefArray( 2 ) = aTmp( 1 )
Else
Err.Raise 4711, "line split", "not 2 elms: |" & Join( aTmp, "|" ) & "|"
End If
>
> That is also how I would do it (very nice explanation, by the way).
>
> But if the user wants to dim to (2) for documentation purposes, he could use
> the following code:
>
>>> Dim prefArray(2)
>>> prefArray(1) = split(objFile.ReadLine, chr(9))(0)
>>> prefArray(2) = split(objFile.ReadLine, chr(9))(1)
>
> or:
>
>>> Dim prefArray(1)
>>> prefArray(0) = split(objFile.ReadLine, chr(9))(0)
>>> prefArray(1) = split(objFile.ReadLine, chr(9))(1)
>
> In addition to being more complex, it is also more likely to result in an
> unchecked error should it be run on a file in the wrong format.
>
> /Al
>
>