3

Here's what I'm trying to do:

T[1]:=5;
T[2]:=3;
.
.
.
T[9]:=20;

Is there a shortcut to achieving this where I can assign the values in a single line?

2 Answers 2

1

I could not find this in any of the documentations but I tried this and it works!

Here's the method used with a complete example:

Program StrangeArray;

Var T: Array[1..5] of Integer = (554,434,144,343,525);
    x:integer;

Begin
For x:=1 to 5 Do
    Begin
    Writeln(T[x]);

    End;

End.

Hope this is useful for others as well.

Sign up to request clarification or add additional context in comments.

2 Comments

@MarcovandeVoort Thank you for this info... This came in a little too late but it's a good find. The docs should have had this under arrays and not under initializing variables... +1 for your efforts :)
see also stackoverflow.com/a/33623534/2600099 and comments (i.e. it doesn't work in some dialects of pascal)
0

The ISO standard 10206 “Extended Pascal” defines the concept of structured‑value literals. They require named data types, anonymous structured data type literals cannot be specified.

program strangerArrays;
    type
        nonupleIntegerSequence = array[1..9] of integer;
    var
        T: nonupleIntegerSequence;
    begin
        T := nonupleIntegerSequence[1: 5; 2: 3; 9: 20; otherwise 0];
    end.

You can further abbreviate things with an initial‑value specification. This does not require a data‑type prefix since the data type at this particular position in your source code is already known.

program strangestArray(output);
    var
        T: array[1..5] of integer value [5: 525; 4: 343; 3: 144; otherwise 0];
    begin
        writeLn(T[5])
    end.

As of 2024 the FreePascal Compiler still intends to, but does not yet fully support the ISO standards. In particular none of the above features are supported, so only the Delphi method works for the time being. You have additional options for “dynamic arrays” but apparently you were satisfied with initializing “static arrays”.


There is one special case: If you want to initialize a packed single‑dimension integer‑indexed array with minimum index of 1 that has char as its base data type (a. k. a string data type), you can (in ISO standard 7185 “Standard Pascal” as well as ISO 10206 “Extended Pascal”) initialize such arrays like this:

program helloWorld(output);
    var
        cheerfulMessage: packed array[1..12] of char;
    begin
        cheerfulMessage := 'Hello world!';
        writeLn(cheerfulMessage)
    end.

This is supported by the FreePascal Compiler, however, in a non‑standard‑compliant way: The ISO standards say that a too short string literal is padded with space characters (' ', ordinal value 32 on systems using ASCII). The FPC deviates from this requirement and pads strings with the NUL character (ordinal value 0). This has implications on < and > string comparisons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.