From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: How to get a 2D arrays range? Date: Sun, 22 Nov 2015 09:09:15 +0200 Organization: Tidorum Ltd Message-ID: References: <75e0a3fe-a4fc-43e5-a773-06d7bb553b38@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Eg1iptEBd16oD3DgbcER3AgTK2Miz405PRcg4xqOT1ipQx5lMF Cancel-Lock: sha1:yVMadLA3chrZ3qrpPhbd3Z2Qxok= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <75e0a3fe-a4fc-43e5-a773-06d7bb553b38@googlegroups.com> Xref: news.eternal-september.org comp.lang.ada:28489 Date: 2015-11-22T09:09:15+02:00 List-Id: 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 . @ .