Recent

Author Topic: [EXPLAINED}Not understanding array of strings  (Read 273 times)

paul9531031

  • New Member
  • *
  • Posts: 13
[EXPLAINED}Not understanding array of strings
« on: April 05, 2020, 07:23:36 pm »
Might some generous expert help me to understand arrays of strings:
I wish to constuct a two column array which holds 3 char strings in the first column and 240 char strings in the second, but I can't find out how to declare an array of strings of fixed length. If I just declare the array to be of type string them the compiler cannot know how much memory to allocate to it. I assume that I can't use an array of type char to store strings, even if they could fit.
Many thanks.
« Last Edit: April 06, 2020, 09:31:09 am by paul9531031 »

jamie

  • Hero Member
  • *****
  • Posts: 2901
Re: Not understanding array of strings
« Reply #1 on: April 05, 2020, 07:31:58 pm »
 A:array of Array of String;   

A[?,?]:= ?

btw, you most likely need to use the SetLength(A, 100,100); etc,,
something like that.
« Last Edit: April 05, 2020, 07:34:44 pm by jamie »
Number 1 at blue screen app creations!

ASerge

  • Hero Member
  • *****
  • Posts: 1558
Re: Not understanding array of strings
« Reply #2 on: April 05, 2020, 07:39:38 pm »
You can declare an array of characters. It is compatible with strings. Since the array elements must be the same, I can suggest the following construction:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. type
  5.   TOneRow = packed record
  6.     Column1: array[0..2] of Char;
  7.     Column2: array[0..239] of Char;
  8.   end;
  9.  
  10.   TWantedArray = array[0..99] of TOneRow;
  11.  
  12. begin
  13.   Writeln(SizeOf(TOneRow)); // 243
  14.   Writeln(SizeOf(TWantedArray)); // 24300
  15.   Readln;
  16. end.
Or use ShortStrings:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. type
  5.   TOneRow = packed record
  6.     Column1: string[3];
  7.     Column2: string[240];
  8.   end;
  9.  
  10.   TWantedArray = array[0..99] of TOneRow;
  11.  
  12. begin
  13.   Writeln(SizeOf(TOneRow)); // 245
  14.   Writeln(SizeOf(TWantedArray)); // 24500
  15.   Readln;
  16. end.

Warfley

  • Full Member
  • ***
  • Posts: 120
Re: Not understanding array of strings
« Reply #3 on: April 05, 2020, 07:48:03 pm »
Using a record of ShortStrings (enforces max length, faster):
Code: Pascal  [Select][+][-]
  1.   TTwoStringsArrays = array of record
  2.     First: ShortString[3];
  3.     Second: ShortString[240];
  4.   end;    
Using a static array (length not enforced, slower):
Code: Pascal  [Select][+][-]
  1. TTwoStringsArrays = array of array[0..1] of String;
Using Map (most flexible):
Code: Pascal  [Select][+][-]
  1. uses fgl;
  2. type
  3.   TString3 = ShortString[3];
  4.   TString240 = ShortString[240];
  5.   TStringMap = specialize TFPGMap<TString3, TString240>;    
« Last Edit: April 05, 2020, 07:50:59 pm by Warfley »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6288
    • wiki
Re: Not understanding array of strings
« Reply #4 on: April 05, 2020, 07:49:29 pm »
You have 2 types of string (with 8bit chars). The term "string" can refer to either one depending on compiler setting "{$H+}" / "{$H-}"

ShortString[n]   => fixed max length of n, with n between 1 and 255

AnsiString  => variable length string.
The variable internally is a pointer, and memory is allocated, re-allocated and deallocated as needed.

Code: Pascal  [Select][+][-]
  1. type
  2.   TStr3 = shortstring[3];
  3.   TStr200 = shortstring[200];
  4.  
Will declare the 2 types of string you need.

But an array (even with multiple dimensions) all elements, in all dimension must be the same type.
So
  Array[n,0]
  Array[n,1]
Both must be of the same type.
You can not have one of them TStr3 and the other TStr255.

Instead you can however have an array of record.
Code: Pascal  [Select][+][-]
  1. MyArray: Array of record
  2.   s3: shortstring[3];
  3.   s200: shortstring[200];
  4. end;
Then you can do
Code: Pascal  [Select][+][-]
  1. SetLength(MyArray, 100); // only one dimension
  2. MyArray[n].s3 := '123';
  3. MyArray[n].s200 := 'abc.........';



In the type declaration, instead of shortstring[20] you can do String[20].
That will always be shortstring, as ansistring can not have a length declaration.

paul9531031

  • New Member
  • *
  • Posts: 13
Re: [EXPLAINED}Not understanding array of strings
« Reply #5 on: April 06, 2020, 09:31:32 am »
Thank you all.

 

TinyPortal © 2005-2018