[Oberon] open array usage

Jörg Straube joerg.straube at iaeth.ch
Sun Jul 11 05:40:29 MEST 2010


It all depends what the real purpose of your question on
"array literals" or in general "structured literals" is.

1) Is it about lazyness of writing single assignments then the
"Init" function mentioned by Chris is the way to go.

You could even refine it and write specific Init function for
certain nbr of array elements. E.g. in case you use a lot of
ARRAYs of size 4 or 6 in your code --> Init4(r0, r1, r2, r3: REAL);
Init6(r0, r1, r2, r3, r4, r5: REAL);

2) If the purpose is to initialize your module with a structured
value e.g. once in the beginning, then most probably the best
way is to read that structured value form a file instead of hardcoding
it in your module.

3) if it's just for test purposes, to check whether your subroutine does
its job you could even do it this way by using a combination of Oberon
and the Oberon system:

MODULE Reals;

IMPORT Out, Oberon, Texts;

PROCEDURE Sum(a: ARRAY OF REAL): REAL;
	VAR i: LONGINT; s: REAL;
	BEGIN
		s :=  0.0; FOR i := 0 TO LEN(a)-1 DO s := s + a[i] END; RETURN s
	END Sum;
	
PROCEDURE Test*;
	VAR arr: POINTER TO ARRAY OF REAL; S: Texts.Scanner;
	
	PROCEDURE R(i: INTEGER);
		VAR r: REAL;
		BEGIN
			Texts.Scan(S); IF S.class = Texts.Real THEN r:= S.x; R(i+1); arr[i] := r ELSE NEW(arr, i) END;
		END R;

	BEGIN
		Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
		R(0);
		Out.Real(Sum(arr^), 10)
	END Test;

BEGIN
END Reals.Test 2.0 4.1 5.6 17.2 ~

Joerg

On 11.07.2010, at 03:57, Chris Burrows wrote:

>> From: oberon-bounces at lists.inf.ethz.ch 
>> [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of 
>> Aubrey.McIntosh at Alumni.UTexas.Net
>> Sent: Sunday, 11 July 2010 10:56 AM
> 
>> 
>> In reading the compiler and system source, I have seen 
>> statements of the form
>> 
>> InitStruct (s1, "this", 42, "that");
>> InitStruct (s2, "another", 43, "item");
>> InitStruct (s3, "more", 44, "again")
>> 
> 
> Yes - that is a good example of the general case where the array is an array
> of something other than a basic type. 
> 
> I have seen a similar 'divide and conquer' approach used when 
> 
> a) the array is an array of a basic type
> b) there are a large number of items
> c) there is no discernible sequence or pattern allowing loop initialisation
> to be used
> 
> e.g. r: ARRAY 200 OF REAL;
> 
> The 'helper' procedure looks like:
> 
> PROCEDURE Init(VAR r: ARRAY OF REAL; offset: INTEGER; d0, d1, d2, d3, d4,
> d5, d6, d7, d8, d9: REAL);
> BEGIN
>  r[offset] := d0; 
>  r[offset+1] := d1;
> ...
> END Init;
> 
> and the initialisation sequence is then something like:
> 
> Init(r,  0,   1.0,  3.0,  5.0,  7.0,  9.0,  2.0,  4.0,  4.1,  2.1,  4.3);
> Init(r, 10,  11.0, 31.0, 15.0, 27.0, 91.0, 42.0, 34.0, 24.1, 12.1, 34.3);
> Init(r, 20,  11.1, 31.6, 14.0, 21.0, 31.0, 42.6, 31.0, 20.1, 11.1, 30.3);
> ....
> ....
> Init(r,190,  11.0, 31.0, 15.0, 27.0, 91.0, 42.0,  4.0,  4.1,  2.1,  4.3);
> 
> What might otherwise have been 200 statements is reduced to 30 statements.
> 
> However, in many cases where you have a large number of constant values used
> for initialisation the numbers are read in from a file, a registry, a
> resource etc. etc. rather than being hard-coded constants.
> 
> Regards,
> Chris Burrows
> CFB Software
> 
> Astrobe: ARM Oberon-07 Development System
> http://www.astrobe.com
> 
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon



More information about the Oberon mailing list