Partially initializing array of records

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Partially initializing array of records

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

ik-6
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
On Tue, Jul 19, 2011 at 20:44, Clay Stuart <[hidden email]> wrote:
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

Rainer Stratmann
In reply to this post by Clay Stuart
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

Clay Stuart
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 <[hidden email]> 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

etrusco
In reply to this post by Clay Stuart
On Tue, Jul 19, 2011 at 2:44 PM, Clay Stuart <[hidden email]> wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

Martin Friebe
In reply to this post by Clay Stuart
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
FPC}@{$ENDIF}EditorOptionsFoldInfoPas[0]), // Freepas
       (Count: 23; Info: {$IFDEF
FPC}@{$ENDIF}EditorOptionsFoldInfoPas[0]), // pas
       (Count:  3; Info: {$IFDEF
FPC}@{$ENDIF}EditorOptionsFoldInfoLFM[0]), // 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Partially initializing array of records

Clay Stuart
Ok.  Thanks everyone.

Clay

On Tue, Jul 19, 2011 at 2:30 PM, Martin <[hidden email]> wrote:
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 FPC}@{$ENDIF}EditorOptionsFoldInfoPas[0]), // Freepas
     (Count: 23; Info: {$IFDEF FPC}@{$ENDIF}EditorOptionsFoldInfoPas[0]), // pas
     (Count:  3; Info: {$IFDEF FPC}@{$ENDIF}EditorOptionsFoldInfoLFM[0]), // 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal