Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-07

Partially initializing array of records

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Clay Stuart

Hello Everyone:

I've got an array of records that looks something like this:

type
      node = record
            foo : array[1..10] of integer;
            bar : array[1..10] of integer;
      end
var
     graph : array[1..5] of node;

begin...

However, the arrays hold different amounts of numbers. So node[1].foo might hold 5 numbers while node[2].foo might hold only 2.

My Question...
Is there a way to initialize these numbers somehow. It seems the compiler won't allow me to partly fill the arrays. If I use them, it appears I have to use them all the way.

Thank you in advance,
Clay

ik
to Clay Stuart

You are talking about dynamic array:

type
      node = record
            foo : array of integer;
            bar : array of integer;
      end
var
     graph : array of node;
SetLength(graph, 5); // Gives you 0..4 elements
SetLength(graph.foo, 10); //Gives you 0..9 elements
...

Ido

Clay Stuart wrote 31 lines
Hello Everyone: I've got an array of records that looks something like this: type node = record foo : array[1..10] of integer; bar : array[1..10] of integer; end var graph : array[1..5] of node; begin... However, the arrays hold different amounts of numbers. So node[1].foo might hold 5 numbers while node[2].foo might hold only 2. My Question... Is there a way to initialize these numbers somehow. It seems the compiler won't allow me to partly fill the arrays. If I use them, it appears I have to use them all the way. Thank you in advance, Clay _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Clay Stuart: Hello Everyone: I've got an array of records that looks something like this: type node =…
Clay Stuart wrote 26 lines
Hello Everyone: I've got an array of records that looks something like this: type node = record foo : array[1..10] of integer; bar : array[1..10] of integer; end var graph : array[1..5] of node; begin... However, the arrays hold different amounts of numbers. So node[1].foo might hold 5 numbers while node[2].foo might hold only 2. My Question... Is there a way to initialize these numbers somehow. It seems the compiler won't allow me to partly fill the arrays. If I use them, it appears I have to use them all the way. Thank you in advance, Clay

I would do it like this.

const cntmax = 5;
var cnt : longint;
procedure init_counter;
begin
 cnt := 0;
end;
procedure init( v1 , v2 , v3 , v4 , v5 , v6 , v7 , v8 , v9 , v10 : integer );
begin
 if cnt < cntmax then begin
  with graph[ cnt ] do begin
   foo[ 1 ] := v1;
   foo[ 2 ] := v2;
    ...
   foo[ 10 ] := v10;
  end;
  inc( cnt );
 end;
end;
init_counter;
init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 );
init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 );
init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 );
init_arr( 34 , 56 , 33 , 0,0,0,0,0,0,0 );
Clay Stuart
to Rainer Stratmann

How do people initialize large amounts of information in practice? Do they just read in files and convert them over dynamically?

(Sorry for the potentially stupid questions. I'm just coming to Pascal.)

Clay

On Tue, Jul 19, 2011 at 1:54 PM, Rainer Stratmann <

Show quoted text 65 lines
wrote: Am Tuesday 19 July 2011 19:44:17 schrieb Clay Stuart: > Hello Everyone: > > I've got an array of records that looks something like this: > > type > node = record > foo : array[1..10] of integer; > bar : array[1..10] of integer; > end > > var > graph : array[1..5] of node; > > begin... > > > However, the arrays hold different amounts of numbers. So node[1].foo > might hold 5 numbers while node[2].foo might hold only 2. > > My Question... > Is there a way to initialize these numbers somehow. It seems the compiler > won't allow me to partly fill the arrays. If I use them, it appears I have > to use them all the way. > > Thank you in advance, > Clay I would do it like this. const cntmax = 5; var cnt : longint; procedure init_counter; begin cnt := 0; end; procedure init( v1 , v2 , v3 , v4 , v5 , v6 , v7 , v8 , v9 , v10 : integer ); begin if cnt < cntmax then begin with graph[ cnt ] do begin foo[ 1 ] := v1; foo[ 2 ] := v2; ... foo[ 10 ] := v10; end; inc( cnt ); end; end; init_counter; init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 ); init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 ); init_arr( 34 , 56 , 33 , 44, 44, 56, .. , .. , .., 77 ); init_arr( 34 , 56 , 33 , 0,0,0,0,0,0,0 ); _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Flávio Etrusco
to Clay Stuart: Hello Everyone: I've got an array of records that looks something like this: type node =…
Clay Stuart wrote 19 lines
Hello Everyone: I've got an array of records that looks something like this: type       node = record             foo : array[1..10] of integer;             bar : array[1..10] of integer;       end var      graph : array[1..5] of node; begin... However, the arrays hold different amounts of numbers.  So node[1].foo might hold 5 numbers while node[2].foo might hold only 2. My Question... Is there a way to initialize these numbers somehow.  It seems the compiler won't allow me to partly fill the arrays.  If I use them, it appears I have to use them all the way. Thank you in advance, Clay

AFAIK no. You'll have to explicitly initialize them with '0'.

Best regards,
Flávio

Martin
to Clay Stuart: How do people initialize large amounts of information in practice? Do they just read in…
Clay Stuart wrote
How do people initialize large amounts of information in practice? Do they just read in files and convert them over dynamically?

There is an example for pre-initialized variable length arrays in ide\editoroptions.pp line 550 and before.

EditorOptionsFoldDefaults: array[TLazSyntaxHighlighter] of TEditorOptionsFoldRecord =
( (Count: 0; Info: nil), // none
(Count: 0; Info: nil), // text
(Count: 23; Info: {$IFDEF
), // Freepas
(Count: 23; Info: {$IFDEF
), // pas
(Count: 3; Info: {$IFDEF
), // lfm

it uses pointers though

   TEditorOptionsFoldInfoList = Array [0..999] of TEditorOptionsFoldInfo;
   PEditorOptionsFoldInfoList = ^TEditorOptionsFoldInfoList;
   TEditorOptionsFoldRecord = record
     Count: Integer;
     Info: PEditorOptionsFoldInfoList;
   end;

The pointer does not care, that it points to an array defined with a different length....
of course, you must ensure not to access elements outside the boundaries, as all the nice things like range-checks will no longer work....

Afaik the array declaration isn't even needed, just using pointers would already do the trick....

Clay Stuart
to Martin

Ok. Thanks everyone.

Clay

Martin wrote 47 lines
On 19/07/2011 19:05, Clay Stuart wrote: > How do people initialize large amounts of information in practice? Do > they just read in files and convert them over dynamically? > There is an example for pre-initialized variable length arrays in ide\editoroptions.pp line 550 and before. EditorOptionsFoldDefaults: array[TLazSyntaxHighlighter] of TEditorOptionsFoldRecord = ( (Count: 0; Info: nil), // none (Count: 0; Info: nil), // text (Count: 23; Info: {$IFDEF ), // Freepas (Count: 23; Info: {$IFDEF ), // pas (Count: 3; Info: {$IFDEF ), // lfm it uses pointers though TEditorOptionsFoldInfoList = Array [0..999] of TEditorOptionsFoldInfo; PEditorOptionsFoldInfoList = ^TEditorOptionsFoldInfoList; TEditorOptionsFoldRecord = record Count: Integer; Info: PEditorOptionsFoldInfoList; end; The pointer does not care, that it points to an array defined with a different length.... of course, you must ensure not to access elements outside the boundaries, as all the nice things like range-checks will no longer work.... Afaik the array declaration isn't even needed, just using pointers would already do the trick.... ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>