constant array without coded number of elements

Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

constant array without coded number of elements

 · 
Posted: 16.06.2014 - 14:06  ·  #1
I am trying to enter constant array of some type, where I would enter all elements in code. This works fine if I enter the number of array elements (like with MyArr: array[1..3] of TPoint), but I will maintain an array in code that will grow in elements. Sometimes I will have to add 5 elements, sometimes more. Right now I have to increase manually higher array index. I have tried various methods trying to avoid this, but no luck. Is there some way to acomplish this? Something like M5Arr: array[] of TPoint?

Code
type
  TPoint = record
    X, Y, Z: integer;
  end;

const
  APoint: TPoint = (X : 0; Y : 2; Z : 4); // compiles
  
  MyArr: array[1..3] of TPoint = ((X: 0; Y: 2; Z: 4), // compiles
                                  (X: 5; Y: 1; Z: 0),
                                  (X: 2; Y: 2; Z: 2));

  M1Arr: array[1..SizeOf(longword) div 2] of byte = (2, 3); // compiles
  
  M2Arr: array[1..High(M2Arr)] of TPoint = ( // internal compiler error
                                  (X: 0; Y: 2; Z: 4),
                                  (X: 5; Y: 1; Z: 0),
                                  (X: 2; Y: 2; Z: 2));

  M3Arr: array[1..Length(M3Arr)] of TPoint = ( // internal compiler error
                                  (X: 0; Y: 2; Z: 4),
                                  (X: 5; Y: 1; Z: 0),
                                  (X: 2; Y: 2; Z: 2));

  M4Arr: array[1..SizeOf(M4Arr) div SizeOf(TPoint)] of TPoint = ( // internal compiler error
                                  (X: 0; Y: 2; Z: 4),
                                  (X: 5; Y: 1; Z: 0),
                                  (X: 2; Y: 2; Z: 2));

  M5Arr: array[] of TPoint = ( // identifier expected error
                                  (X: 0; Y: 2; Z: 4),
                                  (X: 5; Y: 1; Z: 0),
                                  (X: 2; Y: 2; Z: 2));

rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: constant array without coded number of elements

 · 
Posted: 16.06.2014 - 17:07  ·  #2
Hello Avra,

we don't have variants here. Furthermore we have an one-pass compiler.
So most of your constructions will not work.
Very strange is "length(array)". An array doesn't have any length, only sizeOf.
But SizeOf can only work with an already defined type,var/const.

rolf
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: constant array without coded number of elements

 · 
Posted: 17.06.2014 - 00:21  ·  #3
Quote by rh
So most of your constructions will not work.

Well, I will then have to recalculate array length manually after each change. Anyway, hope that something similar to const Arr: array[] of TSomeType =(...) is possible was not big.

Quote by rh
Very strange is "length(array)". An array doesn't have any length, only sizeOf.
But SizeOf can only work with an already defined type,var/const.

While doing a little research about the topic, I have tried few things found. One of them was to see if at compile time I could use Length() of an array demonstrated in runtime use at the end of this article:
http://www.delphibasics.co.uk/Article.asp?Name=Arrays
As you could see from my unfortunate trials, I was pretty desparate... :banghead:
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: constant array without coded number of elements

 · 
Posted: 17.06.2014 - 23:24  ·  #4
Not a lot of help but the compiler can do some calcs for you, so if you have

Code
const
  cArrayLen1 : word = 3;


Myarray[0..cArrayLen1] of point = ....


and you want to add 3, you can put

Code
const
  cArrayLen1 = 3 + 3;


then to add another 5

Code
const
  cArrayLen1 = 3 + 3 + 5;


No very elegant but....
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: constant array without coded number of elements

 · 
Posted: 20.06.2014 - 01:05  ·  #5
Thanks for the effort Merlin, but I was hoping that it could be done in a more convenient way. In Arduino I can write this without specifying number of bytes in advance:
Code
byte arr[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

I know that single pass compiler is not convenient for this, but I was hoping that something already exist since we already have
Code
const arr : array[1..8] of byte = 'FileName.ext';

I wouldn't even mind if I could reserve more then entered array elements (which should not be a problem for single pass compiler), but unfortunately this is not valid since compiler asks for all 8 array elements:
Code
arr : array[1..8] of byte = (11, 22, 33);   //  <<< ERROR


On the other hand, this is not a show stopper so I will learn to live with it.
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   83   97 · Page-Gen-Time: 0.03135s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI