Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > VCL

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 07-06-2018, 11:31 AM
oxygen oxygen is offline
Junior Member
 
Join Date: May 2018
Posts: 5
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
  #2  
Old 07-09-2018, 07:45 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,292
Default

input in repeatfind is not an array of string, but an array of array of string
your record properties are array of string (d..w)
And here you want multiple record properties, therefor array of recordproperty
Then your call should look like:
Code:
repeatFind([p.D,p.E,.....,p.W]);
But I'm not a big fan of the construction (records and pointer referencing). It looks a bit C to me, and in all your code you lose the advantage of OO and dynamics

And I have some issues with understanding the code. Find is normally a function, this will set a single property. So if you call it recursively in repeatFind (which is a function) it will keep overwriting self.prefix. Not sure what the output would be...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:34 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.