Why do I get an error in the line with Redim?
Hans List
Public strFirst As String
Public strSecond As String
Sub CalculateFuzzyMatch()
strFirst = "International"
strSecond = "Interesting"
Dim dist As Integer
dist = LD(strFirst, strSecond)
MsgBox Str(dist)
End Sub
Private Function Minimum(ByVal a As Integer, _
ByVal b As Integer, _
ByVal c As Integer) As Integer
Dim mi As Integer
mi = a
If b < mi Then
mi = b
End If
If c < mi Then
mi = c
End If
Minimum = mi
End Function
Public Function LD(ByVal s As String, ByVal t As String) As
Integer
'*** Compute Levenshtein Distance
Dim d(1 To 2, 1 To 2) As Integer ' matrix
Dim m As Integer ' length of t
Dim n As Integer ' length of s
Dim i As Integer ' iterates through s
Dim j As Integer ' iterates through t
Dim s_i As String ' ith character of s
Dim t_j As String ' jth character of t
Dim cost As Integer ' cost
' Step 1
n = Len(strFirst)
m = Len(strSecond)
If n = 0 Then
LD = m
Exit Function
End If
If m = 0 Then
LD = n
Exit Function
End If
ReDim d(0 To n, 0 To m) As Integer
' Step 2
For i = 0 To n
d(i, 0) = i
Next i
For j = 0 To m
d(0, j) = j
Next j
' Step 3
For i = 1 To n
s_i = Mid$(s, i, 1)
' Step 4
For j = 1 To m
t_j = Mid$(t, j, 1)
' Step 5
If s_i = t_j Then
cost = 0
Else
cost = 1
End If
' Step 6
d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1)
+ 1, d(i - 1, j - 1) + cost)
Next j
Next i
' Step 7
LD = d(n, m)
End Function
Perry - 31 Jan 2007 19:38 GMT
You have already typed the array with
> Dim d(1 To 2, 1 To 2) As Integer ' matrix
Following statement will suffice:
Redim d(0 To n, 0 to m)
Krgrds,
Perry
> Why do I get an error in the line with Redim?
>
[quoted text clipped - 104 lines]
>
> End Function
Hans List - 31 Jan 2007 19:49 GMT
> You have already typed the array with
>> Dim d(1 To 2, 1 To 2) As Integer ' matrix
>
> Following statement will suffice:
>
> Redim d(0 To n, 0 to m)
Hi Perry,
Sorry, but the macro still stops at the Redim line (even
with Option Base set explicitely to 0 or 1).
Regards,
Hans
Jonathan West - 31 Jan 2007 20:05 GMT
>> You have already typed the array with
>>> Dim d(1 To 2, 1 To 2) As Integer ' matrix
[quoted text clipped - 7 lines]
> Sorry, but the macro still stops at the Redim line (even with Option Base
> set explicitely to 0 or 1).
Try changing this line
Dim d(1 To 2, 1 To 2) As Integer ' matrix
to these two lines
Dim d() As Integer ' matrix
ReDim d(1 To 2, 1 To 2)
Also, generally you get better performance if you define variables as Long
rather than as Integer. That is because a Long fits the 32-bit architecture
of most modern CPUs.

Signature
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Perry - 31 Jan 2007 20:13 GMT
A multidimensional dynamic arrays ...
ahhhh
If you don't need to pre-allocation of the array, leave it blank
like in:
Dim d() As Integer ' matrix
'rest of yr code (incl. validating n and m)
'Re-allocate yr array using
>> Redim d(0 To n, 0 to m)
No Option base clause needed here.
Krgrds,
Perry
>> You have already typed the array with
>>> Dim d(1 To 2, 1 To 2) As Integer ' matrix
[quoted text clipped - 11 lines]
>
> Hans
Hans List - 31 Jan 2007 21:38 GMT
> A multidimensional dynamic arrays ...
> ahhhh
[quoted text clipped - 5 lines]
>
> 'Re-allocate yr array using
Jonathan, Perry,
Thank you very much!
Hans