comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: array slices
Date: Tue, 13 May 2003 01:28:09 GMT
Date: 2003-05-13T01:28:09+00:00	[thread overview]
Message-ID: <3EC04A91.3060905@spam.com> (raw)
In-Reply-To: 3EC0494F.A7D67AA4@bellsouth.net

gshibona @ b e l l s o u t h . n e t wrote:
> 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);

This is perhaps better written as

A := (1, 2, 3, 4);

> 
>     -- however, this does not...
>     a(1..4) := c(1)(1..4);
> 
> end ada_2daryslice;

You can only slice a one-dimensional array. C is a two-dimensional 
array, and must have 2 indices. C (1) is meaningless here.

You have 2 choices. Well, you probably have more, but 2 main ones:

1. Use a loop

for I in A'range loop
    A (I) := C (C'First, I);
end loop;

2. Make C a one-dimensional array of one-dimensional arrays (this is not 
the same thing as a two-dimensional array):

type One_D is array (1 .. 4) of Integer;

A : One_D;
C : array (1 .. 2) of One_D;
...
A := C (1);

-- 
Jeff Carter
"Monsieur Arthur King, who has the brain of a duck, you know."
Monty Python & the Holy Grail




  reply	other threads:[~2003-05-13  1:28 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 [this message]
2003-05-13  1:49 ` tmoran
2003-05-13 11:53 ` James S. Rogers
replies disabled

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