Multiple Arrays, String Comparisons

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
green
Posts: 8
Joined: 2008-04-28 13:05:27

Multiple Arrays, String Comparisons

Post 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
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post 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)
green
Posts: 8
Joined: 2008-04-28 13:05:27

Post 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.
PhGrivel
Posts: 7
Joined: 2011-01-27 15:24:25

[REQ] Multiple Arrays, String Comparisons

Post 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.
Post Reply