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,ef01efac83f51e2e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-19 10:25:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.uchicago.edu!newsfeed.cs.wisc.edu!uwm.edu!newsfeed.cs.utexas.edu!geraldo.cc.utexas.edu!not-for-mail From: "Bobby D. Bryant" Newsgroups: comp.lang.ada Subject: Re: looping through variable length array Date: Tue, 19 Mar 2002 12:21:25 -0600 Organization: dis- Message-ID: References: <3c96047c$1@pull.gecm.com> NNTP-Posting-Host: dial-84-12.ots.utexas.edu Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: geraldo.cc.utexas.edu 1016562196 22006 128.83.219.76 (19 Mar 2002 18:23:16 GMT) X-Complaints-To: abuse@utexas.edu NNTP-Posting-Date: Tue, 19 Mar 2002 18:23:16 +0000 (UTC) User-Agent: Pan/0.11.2 (Unix) X-Comment-To: "Stephen Frackelton" Xref: archiver1.google.com comp.lang.ada:21463 Date: 2002-03-19T12:21:25-06:00 List-Id: 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.