View Single Post
  #1  
Old 07-06-2018, 11:31 AM
oxygen oxygen is offline
Junior Member
 
Join Date: May 2018
Posts: 9
Default How to pass array of strings to function?

Hi guys, I am working on a unit, which searches vocabulary of some language for prefixes.

The code is here

Code:
unit Prefixes;

interface

  uses
  WinTypes, ExtCtrls,
  StdCtrls, Controls, Classes,
  Menus, SysUtils, FileCtrl;

  const TEnArrPrefixChars: array[0..11] of char =
        ('d','e','f','i','o','p','q','r','s','t','u','w');
  type TEnArrPrefixes = record
        D: array[0..1] of string;
        E: array[0..0] of string;
        F: array[0..0] of string;
        I: array[0..1] of string;
        O: array[0..1] of string;
        P: array[0..10] of string;
        Q: array[0..0] of string;
        R: array[0..0] of string;
        S: array[0..7] of string;
        T: array[0..5] of string;
        U: array[0..3] of string;
        W: array[0..1] of string;
    end;
  type TCzArr = record
        B: array[0..1] of string;
        D: array[0..0] of string;
        N: array[0..4] of string;
        O: array[0..4] of string;
        P: array[0..8] of string;
        R: array[0..1] of string;
        S: array[0..2] of string;
        T: array[0..5] of string;
        U: array[0..1] of string;
        V: array[0..1] of string;
        Z: array[0..2] of string;
    end;
    type TPrefixWrapper = record
      en: TEnArrPrefixes;
      cz: TCzArr;
    end;

    const LangPrefixes: TPrefixWrapper =
      ( en: ( D: ( 'de','dis' );
              E: ('ex');
              F: ('for');
              I: ('im','in');
              O: ('over','out');
              P: ('para','part','peri','per','pass','ph','poly','posl','pre','pro','put');
              Q: ('qu');
              R: ('re');
              S: ('self','set','sit','sh','sub','sur','super','sol');
              T: ('take','th','touch','tran','tri','turn');
              U: ('under','uni','un','up');
              W: ('wh','work')
            ); // END en
       cz: (  B: ( 'beze','bez' );
              D: ('do');
              N: ('nade','nad','na','ne','ná');
              O: ('ode','od','obe','ob','o');
              P: ('pode','pro','pod','pos','při','přede','před','pře','po');
              R: ('roze','roz');
              S: ('sou','se','s');
              U: ('u','ú');
              V: ('vy','v');
              Z: ('za','ze','z');
            ); // END cz
      ); // END wrapper

    // @For test purposes
    type ArrStr = Array of String;
    type PData = ^ArrStr; // @For test purposes

    type LangGetPrefix = Class
        private
         word: string;
         prefix: string;
        public
        procedure find(arr: Array of string);

        // @For test purposes
        function repeatFind(const Args: array of String):string;


        function get(langId: byte; word: string): String;
        procedure enGet(langId: byte);
    end;

implementation

procedure LangGetPrefix.find(arr:Array of string);
var n: byte;
    i,needleLen: integer;
    found: boolean;
begin
for n:= 0 to length(arr)-1 do
  begin
  found:= false;
  needleLen := length(arr[n]);
  for i:= 1 to needleLen do
    begin
      if arr[n][i] = self.word[i] then
        begin
          if i = needleLen then // compare completed
            begin
              found := true;
              self.prefix := arr[n];
            end;
          continue
        end
      else
        break;
    end;
    if ( found ) then
        exit;
  end;
end;

// Auto-choose language function
// Word to be searched in array of prefixes. Set byte as language.
function LangGetPrefix.get(langId: byte; word: string): String;
begin
  self.word := word;
  if (langId = 0) then
    self.enGet(langId);
  Result := self.prefix;
end;

// // @For test purposes
function LangGetPrefix.repeatFind(const Args: array of String):string;
var
  i: Integer;
begin
  Result := '';
  for I := 0 to length(Args) do
      find(Args[i]);
end;

procedure LangGetPrefix.enGet(langId: byte);
var p: TEnArrPrefixes;
    r: string;
    s: char;
    i: byte;

    // @For test purposes: Pdata
    Parr: array[0..1] of Pdata;
    
begin
  p := LangPrefixes.en;
  s := word[1]; // first char for prefixes ! With suffix, use last char

//  Parr[0] := PData(p.d)^;
//  repeatFind( Parr );

  if s='d' then find(p.d) else
  if s='e' then find(p.e) else
  if s='f' then find(p.f) else
  if s='i' then find(p.i) else
  if s='o' then find(p.o) else
  if s='p' then find(p.p) else
  if s='q' then find(p.q) else
  if s='r' then find(p.r) else
  if s='s' then find(p.s) else
  if s='t' then find(p.t) else
  if s='u' then find(p.u) else
  if s='w' then find(p.w);
end;

end.
Let you see, all the comments with this note:
@For test purposes:
This is the part where I have problem.

I would like to pass strings of arrays which are defined under
TEnArrPrefixes.en... To

repeatFind( Parr ); either like this or
repeatFind( p.d, p.e, etc. );

where p is just shortcut to p := LangPrefixes.en;

So if you check the function

function LangGetPrefix.repeatFind(const Args: array of String):string;

I have defined the array of sting, but I cannot to pass the arrays of strings to the function. Do you have idea how to fix it?

D: array[0..1] of string;
E: array[0..0] of string;
are different lengths.

Edit:
On other side I've got suggestion to use
repeatFind( [@p.d, @p.e] );
and that this works in Tokyo. But I use Delphi 7 and to me the compiler says
Code:
repeatFind( [@p.d, «
[Error] Prefixes.pas(155): Incompatible types: 'String' and 'Pointer'

Last edited by oxygen; 07-06-2018 at 01:30 PM.
Reply With Quote