comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Drummond <brian3@shapes.demon.co.uk>
Subject: Re: need help learning Ada for a modula-2 programmer
Date: Tue, 28 Jan 2014 12:18:33 GMT
Date: 2014-01-28T12:18:33+00:00	[thread overview]
Message-ID: <teNFu.1243$Tx5.592@fx14.fr7> (raw)
In-Reply-To: l90ee99ngsun16ec498p60nm3s0ah29r47@4ax.com

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

  parent reply	other threads:[~2014-01-28 12:18 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-28  1:06 need help learning Ada for a modula-2 programmer agent
2014-01-28  1:33 ` adambeneschan
2014-01-28  1:52 ` Jeffrey Carter
2014-01-28 12:18 ` Brian Drummond [this message]
2014-02-02  2:47   ` agent
2014-02-02  6:09     ` Jeffrey Carter
2014-02-02 15:02       ` agent
2014-02-02 16:00         ` gautier_niouzes
2014-02-02 19:48         ` Jeffrey Carter
2014-02-03  8:24           ` Dmitry A. Kazakov
2014-02-02 17:18     ` Brian Drummond
2014-02-03  0:10       ` agent
2014-02-03  0:36         ` agent
2014-02-03 12:53         ` Brian Drummond
2014-01-28 22:51 ` Jerry
2014-01-29 12:15   ` Mike H
2014-01-29 20:41     ` Jacob Sparre Andersen
2014-01-29 23:52       ` Jeffrey Carter
2014-01-30  9:05         ` Jacob Sparre Andersen
2014-01-30 14:20       ` Mike H
2014-01-30 14:35         ` Bill Findlay
2014-01-30 15:40           ` Mike H
2014-01-30 23:39         ` Jeffrey Carter
2014-01-31 20:16           ` Mike H
2014-01-29 23:52     ` Jeffrey Carter
2014-01-30  1:44       ` Bill Findlay
2014-01-30  2:01         ` Jeffrey Carter
2014-01-30 12:24       ` Simon Wright
2014-01-30 23:38         ` Jeffrey Carter
2014-02-03 23:12     ` agent
2014-02-04  6:10       ` J-P. Rosen
2014-02-04 22:38   ` agent
2014-01-29 16:58 ` Dirk Heinrichs
2014-01-29 20:43   ` Randy Brukardt
2014-01-29 22:53     ` Georg Bauhaus
2014-01-30 12:13       ` Simon Wright
2014-01-30 17:05     ` Dirk Heinrichs
2014-01-30 23:21       ` Randy Brukardt
2014-01-30  4:29   ` Nasser M. Abbasi
2014-01-30  8:45     ` Where to put change descriptions (Was: need help learning Ada for a modula-2 programmer) Jacob Sparre Andersen
2014-01-30  9:53     ` need help learning Ada for a modula-2 programmer Georg Bauhaus
2014-01-30 21:58       ` Randy Brukardt
2014-01-30 16:28     ` Pascal Obry
2014-01-30 17:43       ` Marius Amado-Alves
2014-01-30 18:10       ` Simon Wright
2014-01-30 22:38       ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox