I've been struggling with generating a 2D array from a string. Perhaps
someone can shed some light on the process or point me in the right
direction...

The string in question looks something like this:

1[/field]Penguins[/field][/record]2[/field]Skuas[/field][/record]5[/field]Eagles,
Golden[/field][/record]

I know this is a rather odd string, but suffice it to say its generated
to represent a series of records which cannot be retrieved via typical
ASP/ADO recordset fashion. The environment is classic ASP using
VBScript.

I can readily break this up into a 1D array using
split(str,"[/record]").
But this yields only the 3 combined sets:
1[/field]Penguins[/field]
2[/field]Skuas[/field]
5[/field]Eagles, Golden[/field]

What I'm looking for is a way to make this a 2D array such that:
aryBirds(0,0) = 1
aryBirds(0,1) = Penguins
aryBirds(1,0) = 2
aryBirds(1,1) = Skuas
aryBirds(2,0) = 5
aryBirds(2,1) = Eagles, Golden

Unfortunately my searching thusfar hasn't hit on what looping is
required to get this 2D result.

So far I've tried things like
For i = 0 to Ubound(aryCombinedSets)-1
aryNewSets = split(aryCombinedSets(i),"[/field]")
For j=0 to Ubound(aryNewSets)-1
aryBirds(i,j) = (aryNewSets(j), j)
Next
Next

But these only generate the last 2 values of the array. That is only
the 5 and Eagles, Golden values are retrievable.

I have several similar strings. In each case I know how many fields
exist, but the number of records varies. In each case I need to make a
2D array so that values can be individually manipulated.

Any assistance or pointers in the right direction would be helpful.
Thanks!

Bonnie

Re: 2D VBScript Array from a String w/ Record and Field Delimiters by Richard

Richard
Wed Dec 13 00:32:58 CST 2006

Bonnie wrote:

> I've been struggling with generating a 2D array from a string. Perhaps
> someone can shed some light on the process or point me in the right
> direction...
>
> The string in question looks something like this:
>
> 1[/field]Penguins[/field][/record]2[/field]Skuas[/field][/record]5[/field]Eagles,
> Golden[/field][/record]
>
...<snip>

>
> What I'm looking for is a way to make this a 2D array such that:
> aryBirds(0,0) = 1
> aryBirds(0,1) = Penguins
> aryBirds(1,0) = 2
> aryBirds(1,1) = Skuas
> aryBirds(2,0) = 5
> aryBirds(2,1) = Eagles, Golden

There are probably better ways, but below is a solution. When you split
using "[/record]" and "[/field]" you get blank entries because of the
trailing delimiters. I look for blanks to skip these. Since dynamic arrays
can only have the second dimension ReDim'd, I reversed your row/column
format. Also, I depend on their being only 2 fields per row, or an error
will be raised. If the number of fields changes, you must adjust the first
dimension of arrBirds.
============
Option Explicit
Dim strValue, arrLines, strLine, arrFields
Dim arrBirds(), j, k, strField

strValue =
"1[/field]Penguins[/field][/record]2[/field]Skuas[/field][/record]5[/field]Eagles,
Golden[/field][/record]"

arrLines = Split(strValue, "[/record]")
j = 0
For each strLine in arrLines
If (strLine <> "") Then
arrFields = Split(strLine, "[/field]")
ReDim Preserve arrBirds(1, j)
k = 0
For Each strField In arrFields
If (strField <> "") Then
arrBirds(k, j) = strField
k = k + 1
End If
Next
j = j + 1
End If
Next

For j = 0 To UBound(arrBirds, 2)
Wscript.Echo "arrBirds(0, " & CStr(j) & ") = " & arrBirds(0, j)
Wscript.Echo "arrBirds(1, " & CStr(j) & ") = " & arrBirds(1, j)
Next

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Re: 2D VBScript Array from a String w/ Record and Field Delimiters by NWdev

NWdev
Fri Dec 15 01:58:54 CST 2006

Richard, this was exactly what I needed to move forward.

Thank you for the excellent -- and prompt -- solution. Sorry I hadn't
gotten back to you earlier!

I've adapted your solution to the task ahead with good result and will
be working through the remaining weird strings of mine in similar
fashion.

Your efforts are certainly appreciated. Sometimes its amazing what a
person can't see when pouring over the details of a solution. :o)

Thanks again.

Bonnie