I know that instead of doing the following for a 1D array of Integer, arr,
SetLength(arr, 4);
arr[0] := 11;
arr[1] := 12;
arr[2] := 13;
arr[3] := 13;`
I can also do the above just on one line:
arr := [11, 12, 13, 14];
Now for a 2D array of array of Integer example (with dimensions 2, 2),
arr[0][0] := 11;
arr[0][1] := 12;
arr[1][0] := 13;
arr[1][1] := 14;
Question: Can I also do this on one line something like the following?
arr := [[11, 12], [13, 14]];
Notes:
- I'm not interested in custom-written functions to "make life easier". I'm strictly asking about default Pascal syntax.
- I already know how to work with (initialize) 2D arrays in Pascal via looping. If that's the only way, I don't want to waste your time. Just say that's the case.
Because this is a required field, again, I tried
arr := [[11, 12], [13, 14]];
but it didn't compile. (Got a Type mismatch error.)
ISO standard 10206 “Extended Pascal” defines the construct of an initial‑value specifier.
You can also assign
arrayliterals to a variable. Structured value constructors must be prefixed with a data type:The syntax also provides an
otherwiseclause for all members not explicitly defined.Special case: If you want to initialize a one‑dimensional
arrayof values in the range0throughord(maxChar)in one go, you could specify an adequate string literal. You will then need to applyordwhen readingarraymembers.Obviously this is really “hacky” in nature and not advisable for serious programming. Keep in mind
maxCharis an implementation‑defined constant, so not the same value everywhere.