From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_05,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e30b6046584172fe,start X-Google-Attributes: gid103376,public From: Alon Matas Subject: Help: Dynamic-size arrays using record discriminant. Date: 1998/10/09 Message-ID: <361E3D0C.41AAC13B@classnet.co.il>#1/1 X-Deja-AN: 399717699 Content-Transfer-Encoding: 7bit Organization: Internet Gold Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-10-09T00:00:00+00:00 List-Id: -- I have this problem: -- There is a record with a discriminant (and a default value to the -- discriminant). The record contains a constrained array, and the range -- of the array depends on the discriminant. For example: procedure PROBLEM is type ARRAY_TYPE is array (INTEGER range <>) of NATURAL; type RECORD_TYPE (SIZE: INTEGER:= 5) is record VECTOR: ARRAY_TYPE (1..SIZE); end record; -- Now, I have a variable with the record type: REC: RECORD_TYPE; TEMP: RECORD_TYPE; -- This variable will be explained later. -- The question is how do I change the array size (i.e, the discriminant -- value) WITHOUT loosing its current content. -- For example, lets say it was filled with values: begin for I in REC.VECTOR'RANGE loop REC.VECTOR(I):= I; end loop; -- Now the array contains the values 1, 2, 3, 4 and 5. Lets say I want to -- enlarge the array to 6 cells. Theoretically, I can do this: REC:= (SIZE => 6, VECTOR => (1, 2, 3, 4, 5, 0)); -- But lets say I don't know the content of the first five cells (for example, -- they came from input). I must give SOME value to "VECTOR" (otherwise it's -- not a complete aggregate). Of course, I can do something like this: REC:= (SIZE => 6, VECTOR => (others => 0)); -- ...but then I loose the original content of the array! -- The only solution I could come up with is quite a shabby one. It goes -- like this: TEMP:= (SIZE => 6, VECTOR => (others => 0)); -- Copying the original -- array into a -- temporary array. TEMP.VECTOR(REC.VECTOR'RANGE):= REC.VECTOR; -- Enlarging the -- original -- array. REC:= (SIZE => 6, VECTOR => TEMP.VECTOR); -- Copying the temporary -- array into the -- original array. -- It's working but it's ugly. Is there a better way? -- If you have an idea, please help! -- Thanks! end PROBLEM;