I scraped text from old files and need to get numerical data placed within strings.
Strings look like:
"season: 1983 colony: 23 colony weight: 4 kg yeild: 12 kg
"season: 1983 colony:- colony weight: 5 kg yeild: 14 kg"
I made a function that takes a raw data string and returns an array of integers.
Function getClearBeeData(rawData As Variant) As Integer()
Dim retValue(4) As Integer 'array where each found number stored
Dim strTempString As String 'temporary string to hold current number
Dim i, k As Integer 'i counter for original string, k counter for array position
Dim token As Boolean 'token shows whether previous chars were number
token = False
For i = 1 To Len(rawData) 'go through original string
If IsNumeric(Mid(rawData, i, 1)) Then 'if current char is numeric
strTempString = strTempString & Mid(rawData, i, 1) 'add current char to remporary string
token = True 'show that current position is within numbers
ElseIf Mid(rawData, i, 1) = Chr(45) Then 'in string a symbol "-" can appear
strTempString = "0"
token = True
ElseIf Not IsNumeric(Mid(rawData, i, 1)) And token = True Then 'if current char is not numeric and token shows that previous char was number
retValue(k) = CInt(strTempString) 'convert temporary string to int and write in to the array
k = k + 1 'go to next array position
token = False 'switch token to show that current position is not within numbers
strTempString = "" 'delete stored data from temporary string
End If
Next
If Len(strTempString) > 0 Then
retValue(k) = CInt(strTempString) 'if original string ended with numbers, write that numbers to array
End If
getClearBeeData = retValue
End Function
Test sub to print data.
Sub printClearBeeData()
Dim rawData As String
Dim clearDataArr() As Integer
Dim i As Integer
rawData = "season: 1983 colony: 12 colony weight: - kg yeild: 16 kg"
clearDataArr = getClearBeeData(rawData)
For i = LBound(clearDataArr) To UBound(clearDataArr) - 1
Debug.Print clearDataArr(i)
Next
End Sub
Everything works. Could I do it better? (As I work alone nobody can code review.)
I didn't use regular expressions because I don't know them.