comp.lang.ada
 help / color / mirror / Atom feed
* How to get a 2D arrays range?
@ 2015-11-22  3:31 John Smith
  2015-11-22  5:02 ` Jeffrey R. Carter
  2015-11-22  7:09 ` Niklas Holsti
  0 siblings, 2 replies; 4+ messages in thread
From: John Smith @ 2015-11-22  3:31 UTC (permalink / raw)


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...


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get a 2D arrays range?
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Jeffrey R. Carter @ 2015-11-22  5:02 UTC (permalink / raw)


On 11/21/2015 08:31 PM, John Smith wrote:
> 
> 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?

In ARM K.2, Language-Defined Attributes, we find

185/1
  A'Range
For a prefix A that is of an array type (after any implicit dereference), or
denotes a constrained array subtype:
186
A'Range is equivalent to the range A'First .. A'Last, except that the prefix A
is only evaluated once. See 3.6.2.

and

189/1
  A'Range(N)
For a prefix A that is of an array type (after any implicit dereference), or
denotes a constrained array subtype:
190
A'Range(N) is equivalent to the range A'First(N) .. A'Last(N), except that the
prefix A is only evaluated once. See 3.6.2.

http://www.adaic.org/resources/add_content/standards/12rm/html/RM-K-2.html

-- 
Jeff Carter
"If you think you got a nasty taunting this time,
you ain't heard nothing yet!"
Monty Python and the Holy Grail
23


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get a 2D arrays range?
  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
  2015-11-22  8:58   ` Simon Wright
  1 sibling, 1 reply; 4+ messages in thread
From: Niklas Holsti @ 2015-11-22  7:09 UTC (permalink / raw)


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
       .      @       .

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get a 2D arrays range?
  2015-11-22  7:09 ` Niklas Holsti
@ 2015-11-22  8:58   ` Simon Wright
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2015-11-22  8:58 UTC (permalink / raw)


Niklas Holsti <niklas.holsti@tidorum.invalid> writes:

> This is described in RM 3.6.2.

http://www.ada-auth.org/standards/12rm/html/RM-3-6-2.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-11-22  8:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2015-11-22  8:58   ` Simon Wright

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