comp.lang.ada
 help / color / mirror / Atom feed
From: "Bobby D. Bryant" <bdbryant@mail.utexas.edu>
Subject: Re: looping through variable length array
Date: Tue, 19 Mar 2002 12:21:25 -0600
Date: 2002-03-19T12:21:25-06:00	[thread overview]
Message-ID: <a77vmj$lfm$1@geraldo.cc.utexas.edu> (raw)
In-Reply-To: 3c96047c$1@pull.gecm.com

On Mon, 18 Mar 2002 09:16:00 -0600, Stephen Frackelton wrote:

> i'm attempting to loop through a set of arrays, but theses arrays are
> of different lengths to cater for various message sizes. but i want to
> write the loop only once to cater for this. how is the best way to
> model this ?
> 
> can i somehow use an unconstrained array ?
> 
> if i have my data available in a fixed size array, how can i get this
> variable into a format i can loop through, regardless of the size of
> the array.

Yes, if your arrays are all instances of the same unconstrained type
you simply do -

	for I in A'Range loop
	   Do_Whatever( A( I ) ); -- Process the current element of A.
	end loop;

Of course, your code needs to know which array A is, so you may want to
put the code in a separate procedure or function that accepts an array
of that unconstrained type as an argument, and apply your logic to the
argument as in the snippet above:

	Cater_To( A ); -- process the message in A.
	Cater_To( B ); -- process the message in B.
	Cater_To( C ); -- process the message in C.
	...

Also, it's not clear what you mean by "a set of arrays".  If the logic
of the program makes it sensible to treat your "set" as an array of
arrays, then you can just treat them all in a nested loop rather than
calling a subprogram for each in turn.

Also, again depending on the logic of your program, it may be possible
to use a single unconstrained array to hold messages of whatever size,
and dispense with multiple arrays altogether.  To do this you need to
define the unconstrained type and an access type to point to it, and
instantiate Unchecked_Deallocation to get rid of objects of that type.
Then your code that creates the message just discards any previous
object pointed to by the array variable (which is now actually a
pointer), creates a new array of the required size, and fills in its
contents.  The other part of the code allways processes the "same"
array; it's just not always the same length.


Good luck,

Bobby Bryant
Austin, Texas

p.s. - A moment's thought should reveal what an incredibly robust
saftey feature looping over A'Range will prove to be, in addition to
the convenience issue that motivated your question.  If you change some
constants elsewhere in the program, you don't get a big run-time
explosion because you forgot to change the loop bounds as well.



      parent reply	other threads:[~2002-03-19 18:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-18 15:16 looping through variable length array Stephen Frackelton
2002-03-18 15:40 ` Larry Hazel
2002-03-18 17:47   ` Stephen Frackelton
2002-03-18 18:53     ` Larry Hazel
2002-03-18 15:57 ` Martin Dowie
2002-03-18 21:18 ` Stephen Leake
2002-03-19 18:21 ` Bobby D. Bryant [this message]
replies disabled

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