8

In Java we can initialize an array using following code:

data[10] = {10,20,30,40,50,60,71,80,90,91};

How can we do this in Pascal?

0

2 Answers 2

10

Here’s a full example program that compiles for both Windows and Linux.

program test;
var
    data: array[0..9] of integer = (10,20,30,40,50,60,71,80,90,91);
begin
    writeln('Hello World');
    writeln(data[0]);
end.
Sign up to request clarification or add additional context in comments.

16 Comments

Unfortunately it does not work for Borland Turbo Pascal (nor fpc -Mtp)
@KenWhite because that was a very popular dialect of Pascal, and many programs were written in it. So many (more modern) Pascal compilers (like fpc mentioned above) support that dialect for compiling them. Thus your question boils down to "why don't you rewrite all those programs in another Pascal dialect / another language altogether", to which there are several answers, most notable of which are "wanting github.com/mnalis/ironseed_fpc to stay as close to original as possible" and "limited human lifetime" :)
@KenWhite Well, I linked to the source (written in 1994, so it's barely 30 years old), so feel free to rewrite it in Rust of Elixir or Python or whatever catches your fancy. Let me know when you're done. As for the "old car" analogy, note that nobody is forcing YOU to maintain that old car if YOU don't like it. But also note that ARE people who ARE into maintaining oldtimer cars, as well as there are retrocomputing fans. That might not be your cup of tea, but some people prefer that. Suum cuique, eh? (or "to each his own" if you prefer more modern and maintainable language)
@KenWhite You continuously seem to be conflating "first ever release" of a software (Turbo Pascal 1.0 was released in 1983, so 40+ years) with "software being beyond their expiration date". By that logic, Delphi is now almost 30 years "beyond its expiration date" (being first released ~1995). TP7 was released in 1992, and was definitely not "beyond its expiration date" even then, but at the very peak of its popularity! As for "porting for maintainability", look at any recent devel poll, and (sadly) none will mention ANY Pascal dialect as popular or maintainable or future proof.
@KenWhite You seem to be under incorrect impression that Borland is only entity producing Pascal compiler. I've already mentioned FreePascal.org which I use (and which supports Turbo Pascal dialect!) which is also still under development, with releases every year or so (and is more portable than anything that Borland releases). And just like Delphi dialect "refuses to die", so does the TP dialect. But there are less new Pascal developers every year in polls, so people should be considering porting to something more popular and modern if they care about longer-term maintainability.Move to chat?
|
1

Technically initialization means the first definition (the first := assignment) of a variable, i. e. a transition from the state undefined to defined. Considering your Java code example this answer focuses on a combined declaration/definition.

Standardized Pascal: Extended Pascal

In Extended Pascal as laid out by the ISO standard 10206 a variable declaration may be followed by an initial value specification. It looks like this:

program arrayInitializationDemo;
    var
        data: array[1..10] of integer value [ 1: 10;  2: 20;  3: 30;
              4: 40;  5: 50;  6: 60;  7: 71;  8: 80;  9: 90; 10: 91];
    begin
    end.

Furthermore, you can omit one recurring value with an array completer clause:

    data: array[1..10] of Boolean value [1, 4..6: true; otherwise false];

Now data[1], data[4], data[5] and data[6] are true, any other component of data is false. And certainly you can nest arrays.

Extended Pascal has the really neat feature of associating an initial value specification with the data type. This relieves you from repeating yourself.

program initialStateDemo(output);
    type
        natural = 1..maxInt value 1;
    var
        N: natural;
    begin
        writeLn(N)    { prints `1` }
    end.

This can of course be overridden with a confliciting value clause in the var declaration.

Non‑standardized Pascal: creativity of Pascal dialects

The original Pascal as presented by Niklaus Wirth in The Programming Language Pascal – Revised Report of July 1973 did not provide any means to initialize an array at its declaration site. Various implementers added their own (mutually incompatible) extensions; this list does in no way claim to be exhaustive.

Delphi, FreePascal, …

In some Pascal dialects – including Delphi and FreePascal – only the method showcased by Michael works. As far as I know, the Borland Pascal array initializer does not provide a means to simulate Extended Pascal’s otherwise or repeat the same value for various indices (1, 4..6 in the example above); you need to spell things out.

Turbo Pascal

Turbo Pascal does not support initialized variables at all. For Michael’s example code to work you need to substitute var with const. However, this turns data into a persistent variable, a variable that has an indefinite lifetime (i. e. is initialized only once).

VAX Pascal

VAX Pascal does support initialized variables, yet only for data types you can specify literal values for; unfortunately in VAX Pascal you cannot specify array literals. Note, instead of Extended Pascal’s keyword value VAX Pascal re‑uses :=.

PascalABC.NET

PascalABC.NET uses :=, too, and supports abbreviated initialization for both “static arrays” and “dynamic arrays”.

var
    data: array of integer := (10, 20, 30, 40, 50, 60, 71, 80, 90, 91);

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.