Sign in Join

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

Mailing lists / fpc-pascal / 2017-04

Correct syntax for initialized constants/variables

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Ralf Quint

I try to include some initialized constants/variables in an application of mine, but somehow stumbled upon a stupid little problem that I can't figure out myself after nights on the computer.

I see in the reference docs (section 4.4) examples for arrays and examples for records, but in my case, I would need to properly define an array of records.

Any hint/example for the proper syntax in that case would be deeply appreciated, it's probably something simple that I am missing,

tia,

Ralf

---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Ewald
to Ralf Quint
Ralf Quint wrote 7 lines
I try to include some initialized constants/variables in an application of mine, but somehow stumbled upon a stupid little problem that I can't figure out myself after nights on the computer. I see in the reference docs (section 4.4) examples for arrays and examples for records, but in my case, I would need to properly define an array of records.

Something like this?

Type
	TA = Record
		a: Integer;
	End;

Var
SomeArray: Array[0..1] of TA = (
(a: 42;),
(a: 17;)

	);

Note: this is written from memory, syntax might differ a little (but the error messages from the compiler should help you out in that case).

Ewald
Giuliano Colla
to Ralf Quint: I try to include some initialized constants/variables in an application of mine, but…

Il 27/04/2017 20:49, Ralf Quint ha scritto:

Show quoted text 6 lines
I see in the reference docs (section 4.4) examples for arrays and examples for records, but in my case, I would need to properly define an array of records. Any hint/example for the proper syntax in that case would be deeply appreciated, it's probably something simple that I am missing,

This comes from a working program, and the compiler didn't complain, so I assume it is the correct syntax (the added comments are unnecessary, of course):

Show quoted text 33 lines
type Tentry = record bf: array [0..3] of Integer; sq: array [0..3] of Integer; ls: array [0..3] of Integer; ins:array [0..3] of Integer; end; var TTAB_XX: array[0..3] of Tentry = ( { N=0 } { bf } (bf: (0,0,0,0); { sq } sq: (1,2,0,0); { ls } ls: (0,3,0,0); { ins} ins:(1,0,0,0)), { N=1 } { bf } (bf: (0,0,0,0); { sq } sq: (1,1,0,0); { ls } ls: (3,0,0,0); { ins} ins:(1,0,0,0)), { N=2 } { bf } (bf: (0,0,0,0); { sq } sq: (2,0,0,0); { ls } ls: (3,0,0,0); { ins} ins:(1,0,0,0)), { N=3 } { bf } (bf: (0,0,0,0); { sq } sq: (3,0,0,0); { ls } ls: (3,0,0,0); { ins} ins:(1,0,0,0)));

I hope that it helps,

Giuliano

Ralf Quint
to Ewald: Something like this? Type TA = Record a: Integer; End; Var SomeArray: Array[0..1] of TA =…
Ewald wrote 23 lines
On 27/04/17 20:49, Ralf Quint wrote: > I try to include some initialized constants/variables in an application > of mine, but somehow stumbled upon a stupid little problem that I can't > figure out myself after nights on the computer. > > I see in the reference docs (section 4.4) examples for arrays and > examples for records, but in my case, I would need to properly define an > array of records. Something like this? Type TA = Record a: Integer; End; Var SomeArray: Array[0..1] of TA = ( (a: 42;), (a: 17;) ); Note: this is written from memory, syntax might differ a little (but the error messages from the compiler should help you out in that case).

Well, no, the error messages just state "expected this, found that", without actually giving a REAL hint...

The declaration would be something like this

Type tStateRecord = Record
                                         StateAbbrev : String [4];
                                         StateName   : String [40];
                                         InLower48    : Boolean;
                                    end;
        tStateArray = Array [1..MaxNoStates] of tStateRecord;

Var {or Const, so it's initialized only once}
States : tStateArray = ( (StateAbbrev : 'AL', StateName : 'Alabama', InLower48:TRUE),
(StateAbbrev : 'AK',
StateName : 'Alaska', InLower48:FALSE),
...

                                               );

Various versions of parenthesis just vary the error message, either the above mentioned "expected this, found that" or "filed xyz not initialize"...

Ralf

---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Ralf Quint
to Ewald: Something like this? Type TA = Record a: Integer; End; Var SomeArray: Array[0..1] of TA =…
Ewald wrote 20 lines
On 27/04/17 20:49, Ralf Quint wrote: > I try to include some initialized constants/variables in an application > of mine, but somehow stumbled upon a stupid little problem that I can't > figure out myself after nights on the computer. > > I see in the reference docs (section 4.4) examples for arrays and > examples for records, but in my case, I would need to properly define an > array of records. Something like this? Type TA = Record a: Integer; End; Var SomeArray: Array[0..1] of TA = ( (a: 42;), (a: 17;) );

Never mind, I just found my problem. Quite embarrassing for someone programming in Pascal for 41 years, who should know better as to where to put a comma and where to put a semicolon... :-[ Just picture me slapping myself hard for the next hour or two... :-!

Ralf

---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus