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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.albasani.net!feeder.erje.net!us.feeder.erje.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!fx14.fr7.POSTED!not-for-mail From: Brian Drummond Subject: Re: need help learning Ada for a modula-2 programmer Newsgroups: comp.lang.ada References: User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-ID: NNTP-Posting-Host: 62.49.20.82 X-Complaints-To: abuse@demon.net X-Trace: 1390911513 62.49.20.82 (Tue, 28 Jan 2014 12:18:33 UTC) NNTP-Posting-Date: Tue, 28 Jan 2014 12:18:33 UTC Date: Tue, 28 Jan 2014 12:18:33 GMT X-Received-Bytes: 3537 X-Received-Body-CRC: 2520603274 Xref: news.eternal-september.org comp.lang.ada:18306 Date: 2014-01-28T12:18:33+00:00 List-Id: On Mon, 27 Jan 2014 20:06:43 -0500, agent wrote: > The following code does not compile. I don't understand why. > > The string processing fails to compile, saying "prefix of image must be > a type" > > and > > "move is undefined" > > I don't yet understand string processing with Ada The specific errors have been addressed by others, but as a former M2 fan who has transitioned to Ada, I have a suggestion to help with this last point... Ada allows variable sized arrays (including strings) to be allocated on the stack - PROVIDED the actual runtime size is known at the point of allocation. It also allows the size (or first and last indices) of an array to be queried at runtime. It also allows new declaration regions within blocks of code (funnily enough, beginning with the word "declare"...) Put together, these can greatly reduce the pain of processing with fixed length strings, so that actually using variable length strings is rarer than you might imagine at first sight. So for example you can write MDY2STR as a function: function MDY2STR(M,D,Y : Natural) return String is DateSepChar : constant character := '/'; IntermedStr : constant String := natural'image(M) & DateSepChar & natural'image(D) & DateSepChar & natural'image(Y); -- This works DESPITE the length of the string being unknown at -- compile time, because the values M,D,Y are known, so the length -- can be computed, before each execution of the initialiser -- on each call of MDY2STR. "constant" means it will never be changed -- and its scope (local to MDY2STR) guarantees it will be released on -- return BEGIN return IntermedStr; END MDY2STR; And you can call it in a loop which uses a different length string each time: FixedStr : String255; Year, Month, Day : natural; ... Year := yyy; Month := mmm; for Day in 1 .. Days_In(Month) loop declare varString : String := MDY2STR(M=>Month, D=>Day, Y=> Year); -- this string could be constant. As it isn't we may edit it -- as long as we don't change its length. -- Note named association to prevent D,M,Y/Y,M,D type mistakes! -- Declaring ranged subtypes for month, day would also help begin FixedStr := (others => " "); -- empty fixed string FixedStr(varString'range) := varString; -- assign to some of it end -- now we have interfaced our variable length string to -- an M2 type fixed string end loop; Once you get used to some Ada patterns for string handling, "fixed" strings of runtime-determined length are flexible and easy to use. - Brian