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: Sun, 02 Feb 2014 17:18:44 GMT
Date: 2014-02-02T17:18:44+00:00	[thread overview]
Message-ID: <U5vHu.5552$uW2.3805@fx20.fr7> (raw)
In-Reply-To: b4cre95n8pv60s9jkuubasc6rfkjlh7jp8@4ax.com

On Sat, 01 Feb 2014 21:47:15 -0500, agent wrote:

> On Tue, 28 Jan 2014 12:18:33 GMT, Brian Drummond
> <brian3@shapes.demon.co.uk> wrote:
> 
>>The specific errors have been addressed by others, but as a former M2
>>fan who has transitioned to Ada ...
>>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.

> Brian, I need more help here.  I have another function I am trying to
> get working, that you may recognize from M2 code
> 
> PROCEDURE GetCommandLine(CommandLine : OUT String) is BEGIN
>   n := argument_count;
>   k := 1;
> 
>   WHILE k <= n LOOP
>     move(argument(k),argstr);   -  tried this-
>     argstr := argument(k);         -- and this.  Neither works.
> 
>     cmdlinefrag := cmdlinefrag & argstr;
>     cmdlinefrag := cmdlinefrag & ' ';
>     k := Natural'SUCC(k);
>   END LOOP; -- while k < n CommandLine := cmdlinefrag;
> END GetCommandLine;

> I am getting "wrong length for array of type string" errors.
  
Looking at this code I believe the immediate problem is a fixed length 
"argstr" instead of a newly declared one (of the correct size!) for each 
iteration of the loop. 

But there's a wider problem in that the actual parameter for 
CommandLine : OUT String needs to be correctly sized; or (as a function) 
you need to build a correctly sized string piece by piece.

Jeffrey has shown you a recursive way and there's (generally) nothing 
wrong with that. (If your target system has 128 bytes of RAM, that's 
another matter of course!).

If you want a non-iterative approach, one way is to loop twice, first 
calculating the required length, then declare the required length string 
and loop again filling it. This string can then be returned as the 
function result.

function GetCommandLine return String is 
   length : natural := 0;
BEGIN
   for i in 1 to argument_count loop
      length := length + argument(i)'length;
   end loop;
   length := length + argument_count; -- for the spaces!
   declare 
      result : String (1 .. length);
      pos : natural := 1;
   begin
      for i in 1 to argument_count loop
         result(pos .. pos + argument(i)'length)
            := argument(i) & " ";
         pos := pos + argument(i)'length;         
      end loop;
      return result;
   end;
end GetCommandLine;

> I was able to get this code working using variable length strings.  I
> used bounded strings by copying the code from the texttools.common
> routines.

And finally there is nothing wrong with occasional use of bounded or 
unbounded strings if you decide they are the best tool for the job (or if 
it works and you don't want to mess with it further!) Don't infer that I 
avoid them; they simply aren't usually my first choice!

- Brian


  parent reply	other threads:[~2014-02-02 17: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
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 [this message]
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