comp.lang.ada
 help / color / mirror / Atom feed
From: "chris.danx" <chris.danx@ntlworld.com>
Subject: Re: Arrays and Access types...
Date: Fri, 26 Oct 2001 20:31:22 +0100
Date: 2001-10-26T20:31:22+01:00	[thread overview]
Message-ID: <TRiC7.5325$o56.699876@news2-win.server.ntlworld.com> (raw)
In-Reply-To: 20011025214647.2788a60b.egm2@jps.net


"Eric G. Miller" <egm2@jps.net> wrote in message
news:20011025214647.2788a60b.egm2@jps.net...
>
> New to Ada and trying to figure out how to deal with arbitrary
> slices of arrays.  I defined an array type and an access type
> for it, but I can't seem to figure out how to take a slice of
> the array and assign that slice to an access type.
>
> Type is like:
>
> type My_Array is array (Positive range <>) of Integer;
> type My_Array_Ref is access My_Array;
>
> ... in body ...
>
> left, right, start : My_Array_Ref;
>
> begin
>     start := new My_Array (1 .. 200);
>     left  := start(start'first .. 100); -- that doesn't work!
> ...

[snip]

> If there's a way to do this with regular arrays (no access
> types), that'll work.  I wanted to minimize the number of
> "new" arrays -- I'm not quite comfortable with how/when to
> use Unconstrained_Deallocation yet...

Try this instead to begin with (it's easier)

-- stuff before types
...

-- unconstrained array type
--
type any_old_integer_array is array (positive range <>) of Integer;

-- array type with 200 elements;
--
subtype my_array is any_old_integer_array (1..200);


   start, left : my_array;

begin
   left(start'first..100) := start(start'first .. 100);

end blah_blah;

Slices of arrays need to be the same length, hence the need for the range
specified at both sides of the assignment.

> What I want to do is progressively subset the array, and I
> won't know exactly how it'll get broken up until runtime.  The
> original array will get divvied up into smaller and smaller
> slices of an earlier slice (the remaining can be discarded).

If I understand you correctly, you want to take slices of an array but you
don't know how large those slices will be, but you do know that no slice
will be bigger than the original array.

You can do that as long as the following is satisfied:

left(slice_range) := right(slice_range)

i.e. the range for the slice is the same on both sides.

Just use big enough arrays, and take smaller slices of them ensuring the
above rule.  Later you can use attributes that can help you deal with any
size array, as long as the passed array is a subtype of an unconstrained
array.  Attributes are especially useful for working with strings of
unspecified size.

for example you could define Put like this (if it were not already defined
for you)

procedure put (str : in string) is
begin
   for count in str'range loop
      put (str(count));
   end loop;
end put;


Hope this is helpful,
Chris.










  parent reply	other threads:[~2001-10-26 19:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-26  4:46 Arrays and Access types Eric G. Miller
2001-10-26  5:39 ` James Rogers
2001-10-26  6:45   ` Eric G. Miller
2001-10-26 16:40     ` James Rogers
2001-10-26  6:14 ` tmoran
2001-10-26 14:26 ` Ted Dennison
2001-10-26 19:31 ` chris.danx [this message]
2001-10-26 23:32 ` Jeffrey Carter
2001-10-27  1:08 ` Eric G. Miller
2001-10-27  2:09   ` DuckE
2001-10-27  4:23     ` Steven Deller
2001-10-27 18:30       ` Eric G. Miller
replies disabled

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