Recent

Author Topic: Nested FunctionsQuestions  (Read 3901 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Nested FunctionsQuestions
« on: September 26, 2021, 08:45:27 am »
howardpc wrote this code in answer to a questions I posted on August 27, 2021, in the beginner section titled ' Parsing a really Long string 620 char'.

I have changed the function header to ParseFoLength and adding ALEVEL and two integers A and B. I realize the code has to be modified to parse for lengths.

I'm trying to implement the code segment and getting nowhere. It just exits with this call I make.
 
Var 
  TSA : TStringArray;
  A,B : Integer;
 
 My call: TSA := ParseForLength(StringName, sType, sLevel1, A, B );

  How do I call ParseForLength?

  Thanks


   

Code: Pascal  [Select][+][-]
  1.  function TForm1.ParseForLength(aText: String; const aPhrase: String; ALEVEL : String; ALENGTH, BLENGTH : Integer): TStringArray;
  2.   var
  3.    endPos: SizeInt = 0;
  4.    start: SizeUInt = 0;
  5.  
  6.    procedure AddToResult;
  7.     begin
  8.      SetLength(Result, Succ(Length(Result)));
  9.      case endPos of
  10.        0: Result[High(Result)] := Trim(Copy(aText, start));
  11.        otherwise
  12.          Result[High(Result)] := Trim(Copy(aText, start, endPos-start));
  13.      end;
  14.    end;
  15.    begin
  16.  
  17.   end;          
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Nested FunctionsQuestions
« Reply #1 on: September 26, 2021, 10:20:52 am »
You need to make clearer what you are trying to achieve.
Please give a typical text to be parsed, with its associated phrase and lengths, and what you then expect the output string array to contain.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Nested FunctionsQuestions
« Reply #2 on: September 26, 2021, 10:52:30 am »
howardpc wrote this code in answer to a questions I posted on August 27, 2021, in the beginner section titled ' Parsing a really Long string 620 char'.

He refers to this topic.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Nested FunctionsQuestions
« Reply #3 on: September 26, 2021, 10:58:25 am »
I suppose the function still needs to do what it did in the original post.
So:
  • What is the meaning of the new parameter Level?
  • What is the meaning of the new parameters ALENGHT and BLENGTH?

You are aware that the current function does absolutely nothing, it does not even set a function result?

Bart

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Nested FunctionsQuestions
« Reply #4 on: September 26, 2021, 04:57:42 pm »
@Bart and howaardpc

Bart I don't know how nested functions work or how to call them. Was trying to just get something working so I could figure it out.

OK -  the data entering is a string and could look like the the following:

function CompareStr(const S1 : string; const S2 : string) : Integer; overload;    function CompareStr(const S1 : string; const S2 : string; LocaleOptions:                                             TLocaleOptions) : Integer; overload;


                                                 or

Notes: AnsiContainsString checks whether AText contains ASubText, and returns True if this is the case, or returns False otherwise. The search is performed case-sensitive.
 

    function CompareStr(const S1 : string; const S2 : string) : Integer; overload;

    function CompareStr(const S1 : string; const S2 : string;
                                              LocaleOptions: TLocaleOptions) : Integer; overload;

 
  Notes: AnsiStartsStr checks AText to see whether it starts with ASubText , and
            returns True if it does, False if not. The check is performed case-sensitive.
            Basically, it checks whether the position of ASubText equals 1.

 Again 69 character wide first line indented 2 spaces and remaining lines indented the width of  '  Notes: ' or 9 characters. the word 'Notes:' could change but will always be the first word i.e 'XXX:'


The ALEVEL parm indicates what to break or parse on (function, procedure or ';'. ALENGTH is the indention of the first line and BLENGTH is the indention of the rest of the text in the string.


FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Nested FunctionsQuestions
« Reply #5 on: September 26, 2021, 09:17:15 pm »
And what was wrong with the solutions you got in the other thread?

I have to agree with howardpc:
Give some input strings and the expected output with various values of ALevel, ALength and BLength.
And please make sure that the examples are "clear and obvious" so to speak.

Also, what is the bigger picture here?
What is your application going to do when it's finished?
Your current approach might very well be not the best one.


Nested functions are no different from "normal" function, but they are only accessible from within the procedure or function they are nested in.

Given these declarations:
Code: Pascal  [Select][+][-]
  1. procedure A;
  2. begin
  3. end;
  4.  
  5. procedure B;
  6.   procedure C;
  7.   begin
  8.   end;
  9. begin
  10.   ...
  11.   C;
  12. end;

Now this:
Code: Pascal  [Select][+][-]
  1.   A;
  2.   B;
will compile, but since C is only visible inside B this won't:
Code: Pascal  [Select][+][-]
  1.   A;
  2.   B;
  3.   C;  //<== compilation error

Bart

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Nested FunctionsQuestions
« Reply #6 on: September 26, 2021, 10:21:41 pm »
@Bart
"And what was wrong with the solutions you got in the other thread?"

 I guess nothing except I couldn't get howardpc code to work. and I just figured out how to get your code to work.

  procedure ParseForPhrase(const sToParse, sPhrase: String; SL: TStrings);
 
 The SL : TStrings needed to be changed to TStringList.

 It kinda returns what I want So I'll work on it another week and if no go I'll post an example app.

Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Nested FunctionsQuestions
« Reply #7 on: September 26, 2021, 11:26:36 pm »
  procedure ParseForPhrase(const sToParse, sPhrase: String; SL: TStrings);
 
 The SL : TStrings needed to be changed to TStringList.

No, it does not (in the function deficition), you can call it like this:
Code: Pascal  [Select][+][-]
  1. procedure ParseForPhrase(const sToParse, sPhrase: String; SL: TStrings);
  2. begin
  3. ...
  4. end;
  5.  
  6. ...
  7. var
  8.   SL: TStringList;
  9.   i: integer;
  10. begin
  11.   SL := TStringList.Create;
  12.   try
  13.     ParseForPhrase('function abcde(x: integer): boolean; overrride; foo bar junk ',  'function'; SL: TStrings);
  14.     for i := 0 to SL.Count - 1 do
  15.       writeln(SL[i]);
  16.   finally
  17.     SL.Free;
  18.   end;
  19.   ...
  20. end.

Bart

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Nested FunctionsQuestions
« Reply #8 on: September 27, 2021, 03:14:24 am »
@Bart

I don't know. I was getting a compiler error of item not found on the line
else SL.Add(Trim S)); Changed it to TstringList and it works.       
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Nested FunctionsQuestions
« Reply #9 on: September 27, 2021, 10:03:31 am »
I don't know. I was getting a compiler error of item not found on the line
else SL.Add(Trim S)); Changed it to TstringList and it works.     

The original code I supplied compiles just fine.
Notice that having declared SL as TStrings allows you to call this procedure with all kinds of TStrings derived classes, not just a TStringList.
You could e.g. use it with TMemo.Strings, TComboBox.Items etc.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Nested FunctionsQuestions
« Reply #10 on: September 27, 2021, 10:33:55 am »
Notice that having declared SL as TStrings allows you to call this procedure with all kinds of TStrings derived classes, not just a TStringList.
You could e.g. use it with TMemo.Strings, TComboBox.Items etc.

Bart
Yes, indeed, but OP should note that e.g. Tstringlist functionality that is not in TStrings requires a cast to Tstringlist.
Note Bart:not criticism, I also would use TStrings, but just to point that out.

Example for OP, Sort is not in TStrings.:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. uses classes;
  3. var
  4.  L:Tstrings;
  5. begin
  6.  L:=Tstringlist.create;
  7.  try
  8.    L.Add('3');
  9.    L.Add('1');
  10.    L.Add('2');
  11.    // L.Sort;  //identifier idents no member!!
  12.    if L is TStringlist then Tstringlist(L).Sort;
  13.   writeln(L.text);
  14.  finally
  15.    L.free;
  16.  end;
  17. end.
« Last Edit: September 27, 2021, 10:50:31 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018