comp.lang.ada
 help / color / mirror / Atom feed
From: des walker <des.walker@gecm.com>
Subject: Re: How to make like Fortran "do i = 1,20,2"
Date: 2000/07/27
Date: 2000-07-27T00:00:00+00:00	[thread overview]
Message-ID: <398066A3.8134B965@gecm.com> (raw)
In-Reply-To: 8lpcbe$40n$1@news.uit.no

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






  reply	other threads:[~2000-07-27  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-27  0:00 How to make like Fortran "do i = 1,20,2" Reinert Korsnes
2000-07-27  0:00 ` des walker [this message]
2000-07-27  0:00   ` Gary Scott
2000-07-27  0:00   ` tmoran
2000-07-28  0:00     ` Reinert Korsnes
2000-07-28  0:00       ` Gautier
2000-07-29  0:00       ` tmoran
2000-07-27  0:00 ` Ken Garlington
2000-07-27  0:00 ` Larry Kilgallen
2000-07-27  0:00   ` Gary Scott
2000-07-27  0:00     ` Larry Kilgallen
2000-07-27  0:00     ` Pat Rogers
2000-07-27  0:00     ` Matthew J Heaney
2000-07-28  0:00       ` Gary Scott
2000-07-28  0:00         ` mjsilva
2000-07-29  0:00         ` Ehud Lamm
2000-07-28  0:00           ` Richard Riehle
     [not found]           ` <39833637.3B83BFAC@lmtas.lmco.com>
2000-07-29  0:00             ` Gary Scott
2000-07-29  0:00           ` Robert I. Eachus
2000-07-27  0:00     ` mjsilva
2000-07-27  0:00 ` G. de Montmollin
2000-07-27  0:00 ` Lutz Donnerhacke
replies disabled

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