Recent

Author Topic: Using arrays in routines, etc.  (Read 2617 times)

CM630

  • Hero Member
  • *****
  • Posts: 1197
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Using arrays in routines, etc.
« on: February 22, 2024, 10:47:12 am »
If I do:


Code: Pascal  [Select][+][-]
  1. function a:array of string;
  2. begin
  3. end;  
  4.  
             
it does not compile.
So I usually create a type String1D and then I do

Code: Pascal  [Select][+][-]
  1. function a: String1D;
  2. begin
  3. end;
  4.  
             
This compiles.


But maybe there is a better and an easier way?
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

Fibonacci

  • Hero Member
  • *****
  • Posts: 612
  • Internal Error Hunter
Re: Using arrays in routines, etc.
« Reply #1 on: February 22, 2024, 10:47:50 am »
There isnt.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Re: Using arrays in routines, etc.
« Reply #2 on: February 22, 2024, 10:48:47 am »
Use types.stringdynarray    Most dynamic arrays of basic types are predefined there.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #3 on: February 22, 2024, 12:10:29 pm »
There isnt.
Code: Pascal  [Select][+][-]
  1. procedure foo(bar: array of whatever);
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fibonacci

  • Hero Member
  • *****
  • Posts: 612
  • Internal Error Hunter
Re: Using arrays in routines, etc.
« Reply #4 on: February 22, 2024, 12:11:26 pm »
There isnt.
Code: Pascal  [Select][+][-]
  1. procedure foo(bar: array of whatever);

As a result?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Re: Using arrays in routines, etc.
« Reply #5 on: February 22, 2024, 12:16:51 pm »
There isnt.
Code: Pascal  [Select][+][-]
  1. procedure foo(bar: array of whatever);

That's a different construct, an open array.     Not a dynamic array.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #6 on: February 22, 2024, 12:21:00 pm »
There isnt.
Code: Pascal  [Select][+][-]
  1. procedure foo(bar: array of whatever);

As a result?
Feel free to add "out" infront to know that it's meant to be some kind of result, I usualy do "function x([var/out] y: array of z): boolean" to have a better workflow.
Topic titles "routines" and method "procedure" is one kind of a routine.

That's a different construct, an open array.     Not a dynamic array.
Hmmm it accept static or dynamic, whats your point?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fibonacci

  • Hero Member
  • *****
  • Posts: 612
  • Internal Error Hunter
Re: Using arrays in routines, etc.
« Reply #7 on: February 22, 2024, 12:35:55 pm »
Damn you @KodeZwerg... I wanted to show you something, but I found another INTERNAL ERROR :o :o

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. function func(out values: array of string): boolean;
  4. begin
  5.   values := ['hi']; // project1.lpr(5,10) Error: Assignments to formal parameters and open arrays are not possible
  6.                     // project1.lpr(5,13) Error: Internal error 99080501
  7. end;
  8.  
  9. begin
  10. end.

Its my 4th today!

CM630

  • Hero Member
  • *****
  • Posts: 1197
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Using arrays in routines, etc.
« Reply #8 on: February 22, 2024, 12:38:00 pm »
Use types.stringdynarray
Most dynamic arrays of basic types are predefined there.
I do not like TStringDynArray = array of AnsiString; 
And there is no array of array of string; type there.

But there occurred to be a TStringArray in
sysutils.
« Last Edit: February 22, 2024, 12:46:03 pm by CM630 »
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

cdbc

  • Hero Member
  • *****
  • Posts: 1666
    • http://www.cdbc.dk
Re: Using arrays in routines, etc.
« Reply #9 on: February 22, 2024, 12:40:19 pm »
Hi
Sorry, for the intrusion, but I think it pertains to the thread-title...
I'm a bit uncertain about dynamic arrays as a function result.
If i have this function:
Code: Pascal  [Select][+][-]
  1. function fubar: TStringArray;
  2. begin
  3.   SetLength(Result,5);
  4.   Result[0]:= 'F*cked';
  5.   Result[1]:= 'Up';
  6.   Result[2]:= 'Beyond';
  7.   Result[3]:= 'All';
  8.   Result[4]:= 'Recognition';
  9. end;
then
1) can i use it like: "S:= fubar()[2];" to get 'Beyond', or do I have to assign
    the result to an array variable first?
2) Is the memory allocated to the array managed? or do I have to free it
    somehow?
Btw. TStringArray is defined in sysutils... And as @marcov said most are predefined.
edit: What's stopping you from defining a 'TStringMatrix', e.g.:
Code: Pascal  [Select][+][-]
  1. type
  2.   TStringMatrix = array of TStringArray;
Regards Benny
« Last Edit: February 22, 2024, 12:48:08 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Re: Using arrays in routines, etc.
« Reply #10 on: February 22, 2024, 01:00:48 pm »
Use types.stringdynarray
Most dynamic arrays of basic types are predefined there.
I do not like TStringDynArray = array of AnsiString; 
And there is no array of array of string; type there.

But there occurred to be a TStringArray in
sysutils.

To confuse it more there is also TArray<String>

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #11 on: February 22, 2024, 01:11:29 pm »
Damn you @KodeZwerg... I wanted to show you something, but I found another INTERNAL ERROR :o :o

Its my 4th today!
Lol, shall I congrat?
BTW, since I never used it in such way, was the array initialized?
My conceptual (dynamic) workflow looks like:
Code: Pascal  [Select][+][-]
  1. function Foo(out Bar: array of string): boolean;
  2. begin
  3.   Result := False;
  4.   SetLength(Bar, Succ(Length(Bar));
  5.   Bar[High(Bar)] := 'Foobar';
  6.   Result := True;
  7. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fibonacci

  • Hero Member
  • *****
  • Posts: 612
  • Internal Error Hunter
Re: Using arrays in routines, etc.
« Reply #12 on: February 22, 2024, 01:43:56 pm »
I'm a bit uncertain about dynamic arrays as a function result.
If i have this function:
Code: Pascal  [Select][+][-]
  1. function fubar: TStringArray;
  2. begin
  3.   SetLength(Result,5);
  4.   Result[0]:= 'F*cked';
  5.   Result[1]:= 'Up';
  6.   Result[2]:= 'Beyond';
  7.   Result[3]:= 'All';
  8.   Result[4]:= 'Recognition';
  9. end;
then
1) can i use it like: "S:= fubar()[2];" to get 'Beyond', or do I have to assign
    the result to an array variable first?
2) Is the memory allocated to the array managed? or do I have to free it
    somehow?

If you want to get "F*cked" (pun intended), then yes you can call fubar[0], or fubar[2] to get even "Beyond". AFAIK, it is managed.

I often use construct like "for str in fubar do ...", so it would be bad if the memory wasnt freed. (Or it isnt?)

Lol, shall I congrat?
BTW, since I never used it in such way, was the array initialized?

Yah, thanks, I promoted myself to "Certified Internal Error Hunter" ;)

Initialized.. ":= []" was supposed to be the initialization, of course that doesnt work - hence the error message, but also an internal error.

What I wanted to show you was that if a function returns an array, not as "var/out" but as a result, then you can do "for X in FUNC do ..." which is neat, I do use it often, not only for strings. Code then is more compact, no need to have array variable to store the result.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #13 on: February 22, 2024, 01:51:43 pm »
To confuse it more there is also TArray<String>
TStringList you also not mention, okay it is not a pure array-type but a very nice thing to be used as long you not forget to free it somewhere :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

cdbc

  • Hero Member
  • *****
  • Posts: 1666
    • http://www.cdbc.dk
Re: Using arrays in routines, etc.
« Reply #14 on: February 22, 2024, 02:18:09 pm »
Hi
Quote
okay it is not a pure array-type but a very nice thing to be used as long you not forget to free it somewhere
Yeah, that's just it... That's why I'd like some confirmation, that such an array is indeed managed and auto-free'd  :o
E.g.: for S in astring.Split([',']) do etc...; would be less convenient, if you'd have to somehow free the allocated memory...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018