comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: How to get a 2D arrays range?
Date: Sun, 22 Nov 2015 09:09:15 +0200
Date: 2015-11-22T09:09:15+02:00	[thread overview]
Message-ID: <dbd80rF9il2U1@mid.individual.net> (raw)
In-Reply-To: <75e0a3fe-a4fc-43e5-a773-06d7bb553b38@googlegroups.com>

On 15-11-22 05:31 , John Smith wrote:
> Hello,
>
> I have an array of integers, like so:
>
> ArrayInteger : array (1 .. 10, 1 .. 10) of Integer;
> ...
> ArrayInteger := (others => (others => 0));
> ...
> for iterA in ArrayInteger'Range loop
>    for iterB in 1 .. 10 loop
>      Ada.Integer_Text_IO.Put(ArrayInteger(iterA, iterB));
>    end loop;
>
>    Ada.Text_IO.New_Line;
> end loop;
>
> Of the nested for-loop, where I explicitly call out 1 .. 10, I'd like to specify
> the range more dynamically.  How can I do this?
>
> I tried ArrayInteger(0)'Range, with no success...

Try ArrayInteger'Range(2).

For an N-dimensional array A, A'Range(N) gives the range of the N'th 
dimension. Dimensions are numbered starting from 1.

This is described in RM 3.6.2.

For a multi-dimensional array A, the plain A'Range is equivalent to 
A'Range(1) and gives the range of the first dimension.

For clarity, I would write the outer loop with ArrayInteger'Range(1), 
not the plain 'Range although it means the same, and the inner loop of 
course with ArrayInteger'Range(2).

Your ArrayInteger is a 2-dimensional array, so the expression 
ArrayInteger(0), with a single index, is invalid; an N-dimensional array 
always (in Ada) requires N indices. (Also, zero is not even a valid 
index for either dimension of your array, because the indices start from 1.)

It is of course possible to define a two-dimensional set of numbers as a 
one-dimensional array of "rows", where each row is a one-dimensional 
array of numbers, but for that you must define the row-type as a named type.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

  parent reply	other threads:[~2015-11-22  7:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-22  3:31 How to get a 2D arrays range? John Smith
2015-11-22  5:02 ` Jeffrey R. Carter
2015-11-22  7:09 ` Niklas Holsti [this message]
2015-11-22  8:58   ` Simon Wright
replies disabled

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