comp.lang.ada
 help / color / mirror / Atom feed
* looping through variable length array
@ 2002-03-18 15:16 Stephen Frackelton
  2002-03-18 15:40 ` Larry Hazel
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Stephen Frackelton @ 2002-03-18 15:16 UTC (permalink / raw)


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.





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

* Re: looping through variable length array
  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 15:57 ` Martin Dowie
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Larry Hazel @ 2002-03-18 15:40 UTC (permalink / raw)


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.

Knowing the number of items in the array, just loop from
  array_name'first .. array_name'first + number_items - 1
and you may know that array_name'first is 1, so you can loop from 1 ..
number_items.

I'm a bit confused by "loop through a set of arrays".

Larry



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

* Re: looping through variable length array
  2002-03-18 15:16 looping through variable length array Stephen Frackelton
  2002-03-18 15:40 ` Larry Hazel
@ 2002-03-18 15:57 ` Martin Dowie
  2002-03-18 21:18 ` Stephen Leake
  2002-03-19 18:21 ` Bobby D. Bryant
  3 siblings, 0 replies; 7+ messages in thread
From: Martin Dowie @ 2002-03-18 15:57 UTC (permalink / raw)


"Stephen Frackelton" <stephen.frackelton@baesystems.com> wrote in message
news:3c96047c$1@pull.gecm.com...
> 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.

declare

   procedure Looping_Routine (Message : String) is
   begin
      for Index in Value'Range loop
         -- Do somthing with Message (Index)
      end loop;
   end Looping_Routine;

   Array1 : String (1 .. 10);
   Array2 : String (1 .. 100);

begin
   Looping_Routine (Message => Array1);
   Looping_Routine (Message => Array2);
end;






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

* Re: looping through variable length array
  2002-03-18 15:40 ` Larry Hazel
@ 2002-03-18 17:47   ` Stephen Frackelton
  2002-03-18 18:53     ` Larry Hazel
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen Frackelton @ 2002-03-18 17:47 UTC (permalink / raw)


i would be using 'range anyway
the problem is the array size is variable, i have many declared arrays of
various sizes and wish to write one looping process to cater for all,
obviously i cannot loop through the array name as this only caters for that
instance of the array
thanks
Stephen

"Larry Hazel" <lhhazel@otelco.net> wrote in message
news:3C960A83.C2A1E9E1@otelco.net...
> 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.
>
> Knowing the number of items in the array, just loop from
>   array_name'first .. array_name'first + number_items - 1
> and you may know that array_name'first is 1, so you can loop from 1 ..
> number_items.
>
> I'm a bit confused by "loop through a set of arrays".
>
> Larry





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

* Re: looping through variable length array
  2002-03-18 17:47   ` Stephen Frackelton
@ 2002-03-18 18:53     ` Larry Hazel
  0 siblings, 0 replies; 7+ messages in thread
From: Larry Hazel @ 2002-03-18 18:53 UTC (permalink / raw)


Stephen Frackelton wrote:
> 
> i would be using 'range anyway
> the problem is the array size is variable, i have many declared arrays of
> various sizes and wish to write one looping process to cater for all,
> obviously i cannot loop through the array name as this only caters for that
> instance of the array
> thanks
> Stephen
> 
OK - so put the loop inside of a procedure as Martin suggested and call the
procedure with each array.

Larry



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

* Re: looping through variable length array
  2002-03-18 15:16 looping through variable length array Stephen Frackelton
  2002-03-18 15:40 ` Larry Hazel
  2002-03-18 15:57 ` Martin Dowie
@ 2002-03-18 21:18 ` Stephen Leake
  2002-03-19 18:21 ` Bobby D. Bryant
  3 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2002-03-18 21:18 UTC (permalink / raw)


"Stephen Frackelton" <stephen.frackelton@baesystems.com> writes:

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

Attributes! 

procedure Loop_Thru_Array (Item : in Some_Array_Type) 
is begin
   for I in Item'range loop
      ...
   end loop;
end Loop_Thru_Array;

> can i somehow use an unconstrained array ?

Yes. To make different sized array objects all of the same type, you
use an unconstrained array type.

type Some_Array_Type is array (Some_Index_Type range <> ) of Whatever_Type;

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

Use a slice.

Fixed_Data : Some_Array_Type (1 .. 100);
Fixed_Data_Last : Some_Index_Type := 50;

-- Suppose only 1 .. Fixed_Data_Last has real data

Loop_Thru_Array (Fixed_Data (1 .. Fixed_Data_Last));

Hope this helps.

-- 
-- Stephe



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

* Re: looping through variable length array
  2002-03-18 15:16 looping through variable length array Stephen Frackelton
                   ` (2 preceding siblings ...)
  2002-03-18 21:18 ` Stephen Leake
@ 2002-03-19 18:21 ` Bobby D. Bryant
  3 siblings, 0 replies; 7+ messages in thread
From: Bobby D. Bryant @ 2002-03-19 18:21 UTC (permalink / raw)


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.



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

end of thread, other threads:[~2002-03-19 18:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox