Well, I spent some time putting the algorithms together to see how they can be used. The actual application I will publish in the next post. There are a few shortcomings when it comes down to avoiding duplication which I admit I didn’t think of first but I will fix them quite soon.
Here is the first pass parsing code
Private Sub ParseIt(ByRef strTemplate As String)
Dim icur As Integer = 0
ReDim arrStrings(0)
ReDim arrBelong(0)
arrStrings(0) = ""
arrBelong(0) = 0
' First pass scan
For x = 0 To Len(strTemplate) - 1
Dim strChar As String = strTemplate.Chars(x)
If strChar = "{" Then
Dim iprev As Integer = icur
icur = arrStrings.Length
ReDim Preserve arrBelong(icur - 1)
arrBelong(icur - 1) = iprev
ReDim Preserve arrStrings(icur)
arrStrings(iprev) += "{" + Str(icur) + "}"
arrStrings(icur) = ""
ElseIf strChar = "}" Then
icur = arrBelong(icur - 1)
Else
arrStrings(icur) += strChar
End If
Next
' Clean up the white stuff
For x = 0 To arrStrings.Length - 1
arrStrings(x) = arrStrings(x).Trim
Next
End Sub
Which is done once at the beginning and pulls out the strings that need to be processed on in a template. Although a little tricky keeping track in the arrays is pretty much straightforward.
The spinning however became a little more complex and I kept it reasoably simple to test it out.
Private Function SpinIt(ByVal strPart As String) As String
Dim strParts() As String = strPart.Split("|")
Dim random As New Random()
Dim strResult As String
If strParts.Length > 1 Then
' Choose which bit randomly
strResult = strParts(random.Next(strParts.Length)).Trim
Else
strResult = strPart
End If
' Now process descendants
While strResult.IndexOf("{") > 0
Dim iStart As Integer = strResult.IndexOf("{")
Dim iEnd As Integer = strResult.IndexOf("}")
Dim iLoc As Integer = Val(strResult.Substring(iStart + 1, iEnd - iStart - 1))
strResult = strResult.Substring(0, iStart) + SpinIt(arrStrings(iLoc)) + strResult.Substring(iEnd + 1)
End While
Return strResult
End Function
The fault with this at the moment is in the randomness, admittedly I didn’t put any effort in there, but it does need to be fixed so that two outputs will never be the same.