Page 1 of 1

Multiple Arrays, String Comparisons

Posted: 2010-01-03 06:38:32
by green
Hello,

I'm working on a string comparison function based on the Levenshtein algorithm as a ranking between titles that the user can pick from. This needs a two dimensional array.

I've tried:
d: array[0..10][0..10] of integer;
d: array[0..10, 0..10] of integer;

but both give errors (expecting the 'of' keyword). Looking through other scripts, they define single dimensional arrays only.

How would I define a two dimensional array?

Thanks

Posted: 2010-01-04 16:39:21
by antp
Actually you can't define fixed-size array (I do not think there is any in other scripts?)
You have to define
d: array of array of integer;
and then resize the array "d" with SetLength(d, ...), then resize each of the arrays it contains with SetLength(d, ...)
(to be tested)

Posted: 2010-01-05 00:32:45
by green
Yes, I did look at other scripts in my installation directory first, and didn't see this either.

Thanks. Worked like a charm.

[REQ] Multiple Arrays, String Comparisons

Posted: 2011-03-07 19:18:27
by PhGrivel
Lucky you green !
I get a type mismatch when trying to implement the Array of Array of integer with the SetLength method given by antp.
By the way no other method seems to work.

Did you finaly succeed with your script ?

Here's the code :

Code: Select all

function Levenshtein(strWord1, strWord2: String; CoutSubst: Integer ): Integer;

Var
  Matrix : Array of Array of integer;
  L,C,H,W,R: Integer; // Line, Column, Heigt, Width
  Begin
    H := Length(strWord1);
    W := Length(strWord2);
    SetLength(Matrix, 3); // <-- Here I get a type mismatch without any reason at all to me !
    SetLength(Matrix[0], H + 1);
    SetLength(Matrix[1], W + 1);
    For L := 0 to H do
      Matrix[0, L] := L;
    For C := 1 to W do
      Begin
      Matrix[1, 0] := C;
      For L := 1 to H do
        Begin
          R := Min(Matrix[0, L]+1, Matrix[1, L-1]+1);
          If strWord1[L] = strWord2[C] then
            Matrix[1,L] := Min(R, Matrix[0,L-1])
          Else
            Matrix[1,L] := Min(R, Matrix[0,L-1] + CoutSubst);
        End;
      Matrix[2] := Matrix[0];
      Matrix[0] := Matrix[1];
      Matrix[1] := Matrix[2];
    End;

  Levenshtein := Matrix[0,H];
End;
Can someone help me ?

Thanks.