comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: [Q] Problem with Array Concatenation?
Date: 1997/07/10
Date: 1997-07-10T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001007971857360001@news.ni.net> (raw)
In-Reply-To: 33c280c8.418253@news.demon.co.uk


In article <33c280c8.418253@news.demon.co.uk>, john@assen.demon.co.uk (John
McCabe) wrote:

>procedure Test is
>   type Arr_Type is array (1 .. 100) of Integer;
>
>   Src_Array : Arr_Type;
>   Dst_Array : Arr_Type;
>begin
>   for Index in 1 .. 100 loop
>      Src_Array (Index) := Index;
>   end loop;
>   Dst_Array := (others => 0);
>
>   Dst_Array := Src_Array (51 .. 100) & Src_Array (1 .. 50);
>end Test;

As Tuck pointed out, the concatenation will raise Constraint_Error, even
though this is a legal Ada program.  But even in Ada 83, there is a simple
fix: make the array type unconstrained:

declare
   type AT is array (Positive range <>) of Integer;

   SA : AT (Positive range 1 .. 100);
   DA : AT (SA'Range);
begin
   for Index in SA'Range loop
      SA (Index) := Index;
   end loop;

   DA := SA (51 .. 100) & SA (1 .. 50);
end;

There's another way to make the fix, by manually sliding the initial substring:

declare
   type AT is array (Positive range 1 .. 100) of Integer;

   SA : AT;
   DA : AT;
   subtype Slided is AT (1 .. 50);
begin
   ...
   DA := Slided (SA (51 .. 100)) & SA (1 .. 50);
end;

In Ada 83 the 2nd substring will be automatically slided.

- Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  parent reply	other threads:[~1997-07-10  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-07-08  0:00 [Q] Problem with Array Concatenation? John McCabe
1997-07-08  0:00 ` Michael Quinn
1997-07-09  0:00 ` Tucker Taft
1997-07-10  0:00 ` Matthew Heaney [this message]
1997-07-11  0:00   ` Tucker Taft
1997-07-11  0:00     ` Matthew Heaney
1997-07-13  0:00     ` Robert A Duff
1997-07-13  0:00       ` John McCabe
1997-07-13  0:00     ` Keith Thompson
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox