* array slices
@ 2003-05-13 1:24 gshibona @ b e l l s o u t h . n e t
2003-05-13 1:28 ` Jeffrey Carter
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: gshibona @ b e l l s o u t h . n e t @ 2003-05-13 1:24 UTC (permalink / raw)
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.
To wit...
gnatmake -g -gnatv ada2daryslice.adb
gnatgcc -c -g -gnatv ada2daryslice.adb
GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation,
Inc.
Compiling: ada2daryslice.adb (source file time stamp: 2003-05-13
01:20:09)
17. a(1..4) := c(1)(1..4);
|
>>> too few subscripts in array reference
17 lines: 1 error
gnatmake: "ada2daryslice.adb" compilation error
It's quite possible that I've been looking at this for much too long to
be objective. Thanks in advance
for any help.
Ger
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: array slices
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
2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2003-05-13 1:28 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: array slices
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
2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2003-05-13 1:49 UTC (permalink / raw)
> a : array(1..4) of integer;
> c : array( 1..2, 1..4 ) of integer;
> a(1..4) := c(1)(1..4);
There are two problems here. 1) you can only take slices of one
dimensional arrays (yes, that is annoying). 2) A and C are of different
(anonymous) types so the compiler will point that out if you try to assign
a chunk of one to the other.
If you do
type Rows is array(1 .. 4) of integer;
a : Rows;
c : array(1 .. 2) of Rows;
then any of
a(1 .. 4) := c(1)(1 .. 4);
a := c(1);
a(3 .. 4) := c(2)(1 .. 2);
are ok.
or
type Rows is array(integer range <>) of integer;
a : Rows(1 .. 4);
c : array(1 .. 2) of Rows(1 .. 4);
d : array(5 .. 6) of Rows(11 .. 25);
a(1 .. 2) := d(6)(11 .. 12);
will also work.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: array slices
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
2 siblings, 0 replies; 4+ messages in thread
From: James S. Rogers @ 2003-05-13 11:53 UTC (permalink / raw)
"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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-05-13 11:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox