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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1f8b4cce7e72b123 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-10-26 12:36:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!demon!shale.ftech.net!news.ftech.net!newspeer.clara.net!news.clara.net!news5-gui.server.ntli.net!ntli.net!news2-win.server.ntlworld.com.POSTED!not-for-mail From: "chris.danx" Newsgroups: comp.lang.ada References: <20011025214647.2788a60b.egm2@jps.net> Subject: Re: Arrays and Access types... MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: Date: Fri, 26 Oct 2001 20:31:22 +0100 NNTP-Posting-Host: 62.253.9.45 X-Complaints-To: abuse@ntlworld.com X-Trace: news2-win.server.ntlworld.com 1004124659 62.253.9.45 (Fri, 26 Oct 2001 20:30:59 BST) NNTP-Posting-Date: Fri, 26 Oct 2001 20:30:59 BST Organization: ntlworld News Service Xref: archiver1.google.com comp.lang.ada:15275 Date: 2001-10-26T20:31:22+01:00 List-Id: "Eric G. Miller" 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.