VB6 Equivelant for PHP explode()

SharkBait
02-02-2005, 08:08 AM
Hello again,

I am curious as to what VB6 uses as a function that is the same as PHPs explode()

Example:



$oldstring = "this,that:this2,that2";
$temparray = explode(":", $oldstring); // Produces an array with values (this,that this2,that2)
foreach ($temparray as $tempstring) {
$newarray = explode(",", $tempstring); // Produces values (this that)
next($temparray);
}


What I am doing is somewhat parsing through a field of a database's items that are grouped together and put into the variable $oldstring.

How is this done in Visual Basic? Does the above make sense? Really what I am trying to do is split up this,that and this2, that2 so I can further more split those up into individual components to toss back in my form. Which has to do with sales orders and their items that after the order is created the item, name, date etc are stored in a database.

Perhaps this little explaination of what I am trying to do will allow someone to suggest a better way of going about this. I currently have this working fine with PHP/MySQL in a web intereface :)

Cheers,

SharkBait

00100b
02-02-2005, 08:13 AM
The Split function can be used in this manner. First, split using the semicolon as the delimeter, then split each element of the resulting array using the comma as the delimeter.

SharkBait
02-02-2005, 08:22 AM
Dim s As String
Dim sItems() As String

s$ = "dog,cat,pig,boy,girl,man,woman"
sItems() = Split(s$, ",")



So this kind of thing? It would produce a string array of the items above?

00100b
02-02-2005, 08:33 AM
Yes.

SharkBait
02-02-2005, 09:07 AM
So how do I count the number of entries in an Array?

Say the above was:


Dim s as String
dim i as Integer
Dim strItems() as String
dim strAllItems()

s$ = "Blah,Duh:Blah2,Duh2"

strItems() = split(s$, ":")



How do I count strItems()? in php it would be count($strItems)

that way I can go into another for-loop and split the 2 items in that currently made array?

HardCode
02-02-2005, 09:20 AM
UBound(strItems) will return the last index in the array. Since the array is zero-indexed, it would be:

Dim lCount as Long

lCount = UBound(strItems) + 1


To loop through the array, you don't need the count. Just use:

For X = 0 to UBound(strItems)
' Code
Next X

SharkBait
02-02-2005, 09:34 AM
Ah thanx, I just found that ubound thing and it works nicely.

I just nested for loops to split the array twice and it is working nicely.

Again thank you for the help!

00100b
02-02-2005, 09:46 AM
A note on array bounds.

IMO, it is safer to not assume the base-index of an array. To calculate the number of items in an array, regardless of it's starting index value, I would recommend using a calculation along the lines of:

NumberOfElements = (UBound(arrayvariable) - LBound(arrayvariable)) + 1

loquin
02-02-2005, 10:01 AM
True generally (and it certainly works in this case, ) but SPLIT ignores the Option Base directive, and always uses base 0.

00100b
02-02-2005, 10:09 AM
True, Split creates a base-0 indexed array. But why use different methods "conditionally" based on the source/creator of the array, when a single method will work regardless of the source/creator (at least that I've found).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum