• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

VBscript Array Split Function

T

turtle

#1
Hello,

I would like to create a function within a VBScript where I will enter
a string (i.e person's first name) and that person's first name is
inputted into an array so that each separate character in the array is
a letter of the name inputted.

For example:

Input -- Larry
Array -- [L,a,r,r,y]

I am assumming the Split() function would be the way to go with this,
but I am not 100% sure on the delimiter syntax.

Can anyone help me?
 

My Computer

P

Pegasus [MVP]

#2
"turtle" <dkaloustian@xxxxxx> wrote in message
news:805e227c-38b3-4613-b307-32d88846161a@xxxxxx

> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
From the downloadable help file script56.chm (highly recommended!):

Returns a zero-based, one-dimensional array containing a specified number of
substrings.

Split(expression[, delimiter[, count[, compare]]])

Arguments
expression
Required. String expression containing substrings and delimiters. If
expression is a zero-length string, Split returns an empty array, that is,
an array with no elements and no data.

delimiter
Optional. String character used to identify substring limits. If omitted,
the space character (" ") is assumed to be the delimiter. If delimiter is a
zero-length string, a single-element array containing the entire expression
string is returned.

count
Optional. Number of substrings to be returned; -1 indicates that all
substrings are returned.

compare
Optional. Numeric value indicating the kind of comparison to use when
evaluating substrings. See Settings section for values.

Settings
The compare argument can have the following values:

vbBinaryCompare
0
Perform a binary comparison.

vbTextCompare
1
Perform a textual comparison.
 

My Computer

R

Richard Mueller [MVP]

#3
"turtle" <dkaloustian@xxxxxx> wrote in message
news:805e227c-38b3-4613-b307-32d88846161a@xxxxxx

> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
If the input string is "Larry", there is no delimiter for the Split function
that will break the string into individual letters. I would use the Mid
function and a dynamic array. For example:
========
Option Explicit

Dim strValue, strChar, k, arrChars()

strValue = "Larry"

For k = 1 To Len(strValue)
strChar = Mid(strValue, k, 1)
ReDim Preserve arrChars(k - 1)
arrChars(k - 1) = strChar
Next

For Each strChar In arrChars
Wscript.Echo strChar
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
 

My Computer

M

MikeB

#4
FWIW, the string is already an array of chars and can be accessed by using
the VBScript MID function, treating the array as 1 based.

Dim psuedoARRAY
Dim i
psuedoARRAY = "Larry"


For i = 1 To Len(psuedoARRAY)
MsgBox MID(psuedoARRAY, i, 1)
Next

----- Original Message -----
From: "turtle" <dkaloustian@xxxxxx>
Newsgroups: microsoft.public.scripting.vbscript
Sent: Monday, March 30, 2009 5:48 PM
Subject: VBscript Array Split Function


> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
"turtle" <dkaloustian@xxxxxx> wrote in message
news:805e227c-38b3-4613-b307-32d88846161a@xxxxxx

> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
 

My Computer

#5
> Option Explicit

>
> Dim strValue, strChar, k, arrChars()
>
> strValue = "Larry"
>
> For k = 1 To Len(strValue)
> strChar = Mid(strValue, k, 1)
> ReDim Preserve arrChars(k - 1)
> arrChars(k - 1) = strChar
> Next
>
> For Each strChar In arrChars
> Wscript.Echo strChar
> Next
>
Man, you MS guys always cracks me up with your Systems Hungarian notation.
arrChar() is my favorite. Gee, I though it was a boolean :-)

--
..::[ Hz ]::.
 

My Computer

#6
a = "Larry"

' Make a copy of the string, adding a space after each item
' so we have something to use as a split character
For i = 1 To Len(a)
b = b & Mid(a, i, 1) & " "
Next

' Remove the trailing space
b = Trim(b)

' Split the string into an array
c = Split(b)

' Output the array
For Each item In c
WScript.Echo item
Next

--
..::[ Hz ]::.
 

My Computer

E

ekkehard.horner

#7
turtle schrieb:

> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
To create an array of characters from a string in a language without a specialized
function for this task like VBScript implies that you have to access the characters
in a loop. As evidence: Hz's hack to make the use of Split() possible requires
such a loop to insert delimiters that then have to be transformed to separators
to fit the logic of Split() - an extraordinary waste of space and time.

Richard Mueller's solution is therefore to be preferred; but it can be optimized,
because the size of the array is known before the loop:

UBound( array ) == Len( string ) - 1.

Avoiding the costly ReDim Preserve operation removes a second problem of this code.
For the empty string the result is set to

Dim arrChars()

That statement creates a fixed array with no elements - something that doesn't
make sense and can't be handled properly by VBScript (see output line 6).

Using a RegExp is a second way to solve this problem. For simple cases the
overhead may not be worth while; but for more complex requirements the power
of regular expressions may come handy. E.g.: You can treat duplicated letters
as one element by choosing an appropriate pattern (cf. output line 5).

Code to illustrate:

Dim aTests : aTests = Array( _
Array( "Larry", Array( "L", "a", "r", "r", "y" ) ) _
, Array( "" , Array() ) _
, Array( "L" , Array( "L" ) ) _
)
Dim dicFuncs : Set dicFuncs = CreateObject( "Scripting.Dictionary" )
Set dicFuncs( "RM" ) = GetRef( "RM_S2AC" )
Set dicFuncs( "RMOpt" ) = GetRef( "RM_S2AC_Opt" )
Set dicFuncs( "HZ" ) = GetRef( "HZ_S2AC" )
Set dicFuncs( "ReOne" ) = GetRef( "ReOne_S2AC" )
Set dicFuncs( "ReTwo" ) = GetRef( "ReTwo_S2AC" )
Dim nLine : nLine = 1
Dim aTest
For Each aTest In aTests
Dim sFunc
For Each sFunc In dicFuncs.Keys()
Dim aChars : aChars = dicFuncs( sFunc )( aTest( 0 ) )
Dim vUB
On Error Resume Next
vUB = UBound( aChars )
If 0 <> Err.Number Then vUB = Err.Description
On Error GoTo 0
WScript.Echo Right( " " & nLine, 2 ) _
, ">" & aTest( 0 ) & "<" _
, sFunc _
, "(" & Join( aChars, "," ) & ")" _
, CStr( Join( aChars, "," ) = Join( aTest( 1 ), "," ) ) _
, TypeName( aChars ) _
, vUB
nLine = nLine + 1
Next
Next

Function RM_S2AC( strValue )
Dim k, arrChars()
For k = 1 To Len(strValue)
ReDim Preserve arrChars(k - 1)
arrChars( k - 1 ) = Mid( strValue, k, 1 )
Next
RM_S2AC = arrChars
End Function

Function RM_S2AC_Opt( sText )
Dim nUB : nUB = Len( sText ) - 1
ReDim aRVal( nUB )
Dim nPos
For nPos = 0 To nUB
aRVal( nPos ) = Mid( sText, nPos + 1, 1 )
Next
RM_S2AC_Opt = aRVal
End Function

Function HZ_S2AC( a )
' Make a copy of the string, adding a space after each item
' so we have something to use as a split character
Dim b : b = ""
Dim i
For i = 1 To Len( a )
b = b & Mid( a, i, 1 ) & " "
Next
' Remove the trailing space
b = Trim( b )
' Split the string into an array
HZ_S2AC = Split( b )
End Function

Function ReOne_S2AC( sText )
ReOne_S2AC = Re_S2AC( sText, "." )
End Function

Function ReTwo_S2AC( sText )
ReTwo_S2AC = Re_S2AC( sText, "(.)\1?" )
End Function

Function Re_S2AC( sText, sPat )
Dim oRE : Set oRE = New RegExp
oRE.Global = True
oRE.Pattern = sPat
Dim oMTS : Set oMTS = oRE.Execute( sText )
ReDim aRVal( oMTS.Count - 1 )
Dim nIdx : nIdx = 0
Dim oMT
For Each oMT In oMTS
aRVal( nIdx ) = oMT.Value
nIdx = nIdx + 1
Next
Re_S2AC = aRVal
End Function

output:

=== splitStr2Chr: split string into array of characters ========
1 >Larry< RM (L,a,r,r,y) Wahr Variant() 4
2 >Larry< RMOpt (L,a,r,r,y) Wahr Variant() 4
3 >Larry< HZ (L,a,r,r,y) Wahr Variant() 4
4 >Larry< ReOne (L,a,r,r,y) Wahr Variant() 4
5 >Larry< ReTwo (L,a,rr,y) Falsch Variant() 3
6 >< RM () Wahr Variant() Index außerhalb des gültigen Bereichs
7 >< RMOpt () Wahr Variant() -1
8 >< HZ () Wahr Variant() -1
9 >< ReOne () Wahr Variant() -1
10 >< ReTwo () Wahr Variant() -1
11 >L< RM (L) Wahr Variant() 0
12 >L< RMOpt (L) Wahr Variant() 0
13 >L< HZ (L) Wahr Variant() 0
14 >L< ReOne (L) Wahr Variant() 0
15 >L< ReTwo (L) Wahr Variant() 0
=== splitStr2Chr: 0 done (00:00:00) ============================
 

My Computer

R

Richard Mueller [MVP]

#8
"ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message
news:49d244ea$0$30227$9b4e6d93@xxxxxx-online.net...
turtle schrieb:

> Hello,
>
> I would like to create a function within a VBScript where I will enter
> a string (i.e person's first name) and that person's first name is
> inputted into an array so that each separate character in the array is
> a letter of the name inputted.
>
> For example:
>
> Input -- Larry
> Array -- [L,a,r,r,y]
>
> I am assumming the Split() function would be the way to go with this,
> but I am not 100% sure on the delimiter syntax.
>
> Can anyone help me?
To create an array of characters from a string in a language without a
specialized
function for this task like VBScript implies that you have to access the
characters
in a loop. As evidence: Hz's hack to make the use of Split() possible
requires
such a loop to insert delimiters that then have to be transformed to
separators
to fit the logic of Split() - an extraordinary waste of space and time.

Richard Mueller's solution is therefore to be preferred; but it can be
optimized,
because the size of the array is known before the loop:

UBound( array ) == Len( string ) - 1.

Avoiding the costly ReDim Preserve operation removes a second problem of
this code.
For the empty string the result is set to

Dim arrChars()

That statement creates a fixed array with no elements - something that
doesn't
make sense and can't be handled properly by VBScript (see output line 6).

Using a RegExp is a second way to solve this problem. For simple cases the
overhead may not be worth while; but for more complex requirements the power
of regular expressions may come handy. E.g.: You can treat duplicated
letters
as one element by choosing an appropriate pattern (cf. output line 5).

Code to illustrate:

Dim aTests : aTests = Array( _
Array( "Larry", Array( "L", "a", "r", "r", "y" ) ) _
, Array( "" , Array() ) _
, Array( "L" , Array( "L" ) ) _
)
Dim dicFuncs : Set dicFuncs = CreateObject( "Scripting.Dictionary" )
Set dicFuncs( "RM" ) = GetRef( "RM_S2AC" )
Set dicFuncs( "RMOpt" ) = GetRef( "RM_S2AC_Opt" )
Set dicFuncs( "HZ" ) = GetRef( "HZ_S2AC" )
Set dicFuncs( "ReOne" ) = GetRef( "ReOne_S2AC" )
Set dicFuncs( "ReTwo" ) = GetRef( "ReTwo_S2AC" )
Dim nLine : nLine = 1
Dim aTest
For Each aTest In aTests
Dim sFunc
For Each sFunc In dicFuncs.Keys()
Dim aChars : aChars = dicFuncs( sFunc )( aTest( 0 ) )
Dim vUB
On Error Resume Next
vUB = UBound( aChars )
If 0 <> Err.Number Then vUB = Err.Description
On Error GoTo 0
WScript.Echo Right( " " & nLine, 2 ) _
, ">" & aTest( 0 ) & "<" _
, sFunc _
, "(" & Join( aChars, "," ) & ")" _
, CStr( Join( aChars, "," ) = Join( aTest( 1 ),
"," ) ) _
, TypeName( aChars ) _
, vUB
nLine = nLine + 1
Next
Next

Function RM_S2AC( strValue )
Dim k, arrChars()
For k = 1 To Len(strValue)
ReDim Preserve arrChars(k - 1)
arrChars( k - 1 ) = Mid( strValue, k, 1 )
Next
RM_S2AC = arrChars
End Function

Function RM_S2AC_Opt( sText )
Dim nUB : nUB = Len( sText ) - 1
ReDim aRVal( nUB )
Dim nPos
For nPos = 0 To nUB
aRVal( nPos ) = Mid( sText, nPos + 1, 1 )
Next
RM_S2AC_Opt = aRVal
End Function

Function HZ_S2AC( a )
' Make a copy of the string, adding a space after each item
' so we have something to use as a split character
Dim b : b = ""
Dim i
For i = 1 To Len( a )
b = b & Mid( a, i, 1 ) & " "
Next
' Remove the trailing space
b = Trim( b )
' Split the string into an array
HZ_S2AC = Split( b )
End Function

Function ReOne_S2AC( sText )
ReOne_S2AC = Re_S2AC( sText, "." )
End Function

Function ReTwo_S2AC( sText )
ReTwo_S2AC = Re_S2AC( sText, "(.)\1?" )
End Function

Function Re_S2AC( sText, sPat )
Dim oRE : Set oRE = New RegExp
oRE.Global = True
oRE.Pattern = sPat
Dim oMTS : Set oMTS = oRE.Execute( sText )
ReDim aRVal( oMTS.Count - 1 )
Dim nIdx : nIdx = 0
Dim oMT
For Each oMT In oMTS
aRVal( nIdx ) = oMT.Value
nIdx = nIdx + 1
Next
Re_S2AC = aRVal
End Function

output:

=== splitStr2Chr: split string into array of characters ========
1 >Larry< RM (L,a,r,r,y) Wahr Variant() 4
2 >Larry< RMOpt (L,a,r,r,y) Wahr Variant() 4
3 >Larry< HZ (L,a,r,r,y) Wahr Variant() 4
4 >Larry< ReOne (L,a,r,r,y) Wahr Variant() 4
5 >Larry< ReTwo (L,a,rr,y) Falsch Variant() 3
6 >< RM () Wahr Variant() Index außerhalb des gültigen Bereichs
7 >< RMOpt () Wahr Variant() -1
8 >< HZ () Wahr Variant() -1
9 >< ReOne () Wahr Variant() -1
10 >< ReTwo () Wahr Variant() -1
11 >L< RM (L) Wahr Variant() 0
12 >L< RMOpt (L) Wahr Variant() 0
13 >L< HZ (L) Wahr Variant() 0
14 >L< ReOne (L) Wahr Variant() 0
15 >L< ReTwo (L) Wahr Variant() 0
=== splitStr2Chr: 0 done (00:00:00) ============================

--------------

I agree that you can ReDim the array ahead of time and this is more
efficient than ReDim'ing after processing each character. However, I think
an empty array is useful. If the input string is empty, UBound will be -1,
and the For Each loop will not echo anything, but will also not raise an
error. For example:
=========
Option Explicit
Dim strValue, strChar, k, arrChars()

strValue = "Larry"
strValue = ""

ReDim arrChars(Len(strValue) - 1)

For k = 1 To Len(strValue)
strChar = Mid(strValue, k, 1)
arrChars(k - 1) = strChar
Next

For Each strChar In arrChars
Wscript.Echo strChar
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
 

My Computer

E

ekkehard.horner

#9
Richard Mueller [MVP] schrieb:

> "ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message
> news:49d244ea$0$30227$9b4e6d93@xxxxxx-online.net...
> turtle schrieb:

>> Hello,
>>
>> I would like to create a function within a VBScript where I will enter
>> a string (i.e person's first name) and that person's first name is
>> inputted into an array so that each separate character in the array is
>> a letter of the name inputted.
[...]

> Richard Mueller's solution is therefore to be preferred; but it can be
> optimized,
> because the size of the array is known before the loop:
>
> UBound( array ) == Len( string ) - 1.
>
> Avoiding the costly ReDim Preserve operation removes a second problem of
> this code.
> For the empty string the result is set to
>
> Dim arrChars()
>
> That statement creates a fixed array with no elements - something that
> doesn't
> make sense and can't be handled properly by VBScript (see output line 6).
[...]

> Code to illustrate:
[...]

> On Error Resume Next
> vUB = UBound( aChars )
> If 0 <> Err.Number Then vUB = Err.Description
> output:
[...]

> 6 >< RM () Wahr Variant() Index außerhalb des gültigen Bereichs
[...]

>
> I agree that you can ReDim the array ahead of time and this is more
> efficient than ReDim'ing after processing each character. However, I think
> an empty array is useful. If the input string is empty, UBound will be -1,
> and the For Each loop will not echo anything, but will also not raise an
> error. For example:
> =========
> Option Explicit
> Dim strValue, strChar, k, arrChars()
>
> strValue = "Larry"
> strValue = ""
>
> ReDim arrChars(Len(strValue) - 1)
>
> For k = 1 To Len(strValue)
> strChar = Mid(strValue, k, 1)
> arrChars(k - 1) = strChar
> Next
>
> For Each strChar In arrChars
> Wscript.Echo strChar
> Next
"Dim arrChars()" creates a fixed empty array - you can't let it grow,
because it is fixed and you can't store anything in it, because it
has no slots. That even VBScript does not like such abominations can
be seen by the error thrown as soon as you try to access its UBound.

The name "arrChars" will be reused when you create the dynamic array with
(currently) Len(strValue) slots. If you just use the ReDim - which is
perfectly ok with Option Explicit - you avoid

the misleading insinuation of using a fixed array - as in
case of "Dim arrChars( <NiceNumber> )"

whatever action VBScript has to take to create that beast (for
nothing)

the risk of (mis)using arrChars() before its re-definition
 

My Computer

#10
ekkehard.horner wrote:

> turtle schrieb:

>> Hello,
>>
>> I would like to create a function within a VBScript where I will enter
>> a string (i.e person's first name) and that person's first name is
>> inputted into an array so that each separate character in the array is
>> a letter of the name inputted.
>>
>> For example:
>>
>> Input -- Larry
>> Array -- [L,a,r,r,y]
>>
>> I am assumming the Split() function would be the way to go with this,
>> but I am not 100% sure on the delimiter syntax.
>>
>> Can anyone help me?
>
> To create an array of characters from a string in a language without a
> specialized
> function for this task like VBScript implies that you have to access the
> characters
> in a loop. As evidence: Hz's hack to make the use of Split() possible
> requires
> such a loop to insert delimiters that then have to be transformed to
> separators
> to fit the logic of Split() - an extraordinary waste of space and time.
I never intended my code to be industrial grade since he only needed 5
chars converted to an array.

--
..::[ Hz ]::.
 

My Computer