comp.lang.ada
 help / color / mirror / Atom feed
From: "James S. Rogers" <jimmaureenrogers@worldnet.att.net>
Subject: Re: array slices
Date: Tue, 13 May 2003 11:53:59 GMT
Date: 2003-05-13T11:53:59+00:00	[thread overview]
Message-ID: <r15wa.81710$cO3.5441878@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 3EC0494F.A7D67AA4@bellsouth.net

"gshibona @ b e l l s o u t h . n e t" <gshibona@bellsouth.net> wrote in
message news:3EC0494F.A7D67AA4@bellsouth.net...
> I wonder if someone could point to the error in this code.  I'm looking
> to fill array a with a "row" slice
> of array c.   There doesn't appear to be much info on this particular
> facet of Ada, yet what I have seen
> suggests that the syntax below is correct.
>
> procedure ada_2daryslice is
>
> a : array(1..4) of integer;
> c : array( 1..2, 1..4 ) of integer;
>
> begin
>     c :=  (
>          1 => (0, 1, 2, 4 ),
>          2 => (8, 16, 32, 64)
>          );
>     -- this seems to yield the expected results
>     a(1..4) := ( 1,2,3,4);
>
>     -- however, this does not...
>     a(1..4) := c(1)(1..4);
>
> end ada_2daryslice;
>
> The compiler complains of too few subscripts in the array reference.

Others have noted that you can only slice a one dimensional array.
I will focus on another issue in your code.

This error message is correct. You defined array c as a two dimensional
array. You are accessing it as an array of arrays. Those are two different
concepts in Ada.

To access the elemets of array c you want to use the following syntax:

   c(1,1)

Note that this is different from

  c(1)(1)

Your problem can be better resolved using an array of arrays.

type Row is array (1..4) of integer;
type Matrix is array (1..2) of Row;

a : Row := (1,2,3,4);
c : Matrix := (1 =>(0,1,2,4),
                     2 => (8,16,32,64));

a := c(1);

This works because each element of C is a Row as defined above.
A is also a Row. You can also do the following:

a(1..2) := c(2)(3..4);

Jim Rogers





      parent reply	other threads:[~2003-05-13 11:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-13  1:24 array slices gshibona @ b e l l s o u t h . n e t
2003-05-13  1:28 ` Jeffrey Carter
2003-05-13  1:49 ` tmoran
2003-05-13 11:53 ` James S. Rogers [this message]
replies disabled

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