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-Thread: 103376,f3405cf75879a8dc X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!weretis.net!feeder4.news.weretis.net!news.tornevall.net!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: basic questions on using Ada arrays Date: Wed, 06 Oct 2010 10:03:19 -0700 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: NNTP-Posting-Host: f56e75653ae25b0d7b7d00703a7218cb Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: 91ce38aa113d630d95eadaae37c1ec60 X-Complaints-To: abuse@tornevall.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.12) Gecko/20100915 Thunderbird/3.0.8 X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: X-UserIDNumber: 1738 X-Validate-Post: http://news.tornevall.net/validate.php?trace=91ce38aa113d630d95eadaae37c1ec60 X-Complaints-Italiano: Non abbiamo padronanza della lingua italiana - se mandate una email scrivete solo in Inglese, grazie X-Posting-User: 0243687135df8c4b260dd4a9a93c79bd Xref: g2news1.google.com comp.lang.ada:14416 Date: 2010-10-06T10:03:19-07:00 List-Id: On 10/06/2010 08:43 AM, Nasser M. Abbasi wrote: > > Also, is there a way to initialize an array using a formula or a > function call? I know I can do > > x : array(1..nPoints) of float := (others => 0.0) > > But I wanted to make each entry in the array to have some value > depending on the index value. I did not know how, so that is what the > above small loop does, to initialize x array. Certainly, but one must use a named array type, not anonymous types as you have done. You can find examples of such functions in Ada.Strings.Fixed (ARM A.4.3). Type String is simply an array type. For example X : String := 5 * 'x'; results is X having the subtype String (1 .. 5) and the value "xxxxx". This doesn't give a different value for each component, but it's easy enough to write one yourself: function Digit_List (Length : in Natural) return String; -- Returns a value of subtype String (1 .. Length) containing repeated -- sequences of "1234567890". Each position contains the value of its -- index modulo 10. function Digit_List (Length : in Natural) return String is function Last_Digit (Value : in Positive) return Character; -- returns the Character representation of the last digit in the -- base 10 image of Value. function Last_Digit (Value : in Positive) return Character is Image : constant String := Integer'Image (Value); begin -- Last_Digit return Image (Image'Last); end Last_Digit; Result : String (1 .. Length); begin -- Digit_List All_Digits : for I in Result'range loop Result (I) := Last_Digit (I); end loop All_Digits; return Result; end Digit_List; Column_Header : constant String := Digit_List (80); Of course, all of this requires you to write loops. Loops may be "out of fashion", but Ada isn't about following fashion; if it were, it would look like C, not have all those inefficient run-time checks, and give far fewer compilation errors. -- Jeff Carter "Don't knock masturbation. It's sex with someone I love." Annie Hall 45