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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e511f3ccb3da24af X-Google-Attributes: gid103376,public From: des walker Subject: Re: How to make like Fortran "do i = 1,20,2" Date: 2000/07/27 Message-ID: <398066A3.8134B965@gecm.com>#1/1 X-Deja-AN: 651469111 Content-Transfer-Encoding: 7bit References: <8lpcbe$40n$1@news.uit.no> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: 27 Jul 2000 17:40:40 GMT, farwkn6911.frlngtn.gecm.com Organization: Alenia-Marconi Systems MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-07-27T00:00:00+00:00 List-Id: Reinert Korsnes wrote: > Hi, > > How can one in Ada elegantly make a loop through each 2 element of an array ? > I am looking for something like: "do i = 1,20,2" in Fortran, or > > for I in A'range ("step 2") loop > something... > end loop; > > Yes, I can use: > > I := A'first; > while I <= A'Last loop; > something.. > I := I + 2; > end loop; > > But this does not look so elegant.... > > reinert > > -- > Norwegian Polar Institute > Polar Environment Center > N-9296 Tromso > Norway > Fax: +47 77750501 > > http://geophys.npolar.no/~reinert/personal.html Often wanting to process data in this manner would suggest that there might be a more meaningful way to express the structure of the data. A lot of effort can be spent in Ada designing good data types which makes it simple to write safe code. If the two element blocks of your array hold, say, a coordinate pair you might have type coordinate is record X,Y : integer; end record; type coordinate_array is array(Natural range <>) of coordinate; then processing the array reduces to procedure Process_Array(The_Array : in coordinate_array) is begin for I in The_Array'first .. The_Array'last loop Do_Something(The_Array(I)); end loop; end Process_Array; Now the elegance is returned to the loop block. An earlier respondent also mentioned that there is no guarantee that your original array is an even number of elements long, here the problem is neatly avoided because the array will consist of complete elements. I realise that it is not always possible to backtrack on data structures that are already defined, but Data Abstraction in Ada is a good tool for writing safe, extensible code. Des Alenia-Marconi Systems