From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Beginners question Date: Fri, 12 Nov 2021 17:24:17 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net q6Oo0sJVudDHS8kZwZqqlwyCeMlaPMlX3/8J/lf+IGJ/xj9WKy Cancel-Lock: sha1:+jmbMghVrqKApahyI2QqUgDls5Q= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:63112 List-Id: On 2021-11-12 16:32, uq5huo...@gmail.com wrote: > Hi, > > I'm beginning to play with Ada, and run into this > > with Ada.Text_IO; use Ada.Text_IO; > procedure Learn is > subtype Alphabet is Character range 'A' .. 'Z'; > begin > Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last); > end Learn; > > Now I want to play a bit with the code ... and this fails and I don't get why and how to solve. > > Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last-1); > or > Put_Line ("Learning Ada from " & Alphabet'First & " to " & (Alphabet'Last-1)); > > Anyone more expirienced can explain this to my, please? The Character type is not an integer type (unlike "char" in C), so you cannot subtract 1 from a Character (or an Alphabet value). Character is an enumeration type in Ada. To get the Alphabet value immediately preceding Alphabet'Last, use the Pred ("predecessor") attribute function: Alphabet'Pred (Alphabet'Last) If you need to take larger strides in the Alphabet enumeration, you can use the Pos and Val attribute functions. Pos returns the "position number" of its argument, and that is an integer. Val performs the opposite mapping. For example, to get the Alphabet value that is three positions before Alphabet'Last, you can do Alphabet'Val (Alphabet'Pos (Alphabet'Last) - 3)