constant array without coded number of elements

Avra
Schreiberling
Avatar
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

constant array without coded number of elements

 · 
Gepostet: 16.06.2014 - 14:06 Uhr  ·  #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
Geschlecht:
Herkunft: Germany
Alter: 25
Homepage: e-lab.de
Beiträge: 5558
Dabei seit: 03 / 2002
Betreff:

Re: constant array without coded number of elements

 · 
Gepostet: 16.06.2014 - 17:07 Uhr  ·  #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
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: constant array without coded number of elements

 · 
Gepostet: 17.06.2014 - 00:21 Uhr  ·  #3
Zitat geschrieben von 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.

Zitat geschrieben von 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
Geschlecht:
Alter: 25
Beiträge: 1474
Dabei seit: 03 / 2005
Betreff:

Re: constant array without coded number of elements

 · 
Gepostet: 17.06.2014 - 23:24 Uhr  ·  #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
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: constant array without coded number of elements

 · 
Gepostet: 20.06.2014 - 01:05 Uhr  ·  #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.
Gewählte Zitate für Mehrfachzitierung:   0

Registrierte in diesem Topic

Aktuell kein registrierter in diesem Bereich

Die Statistik zeigt, wer in den letzten 5 Minuten online war. Erneuerung alle 90 Sekunden.
MySQL Queries: 14 · Cache Hits: 14   83   97 · Page-Gen-Time: 0.02482s · Speichernutzung: 2 MB · GZIP: ein · Viewport: SMXL-HiDPI