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: border2.nntp.dca.giganews.com!nntp.giganews.com!goblin3!goblin2!goblin.stu.neva.ru!cyclone03.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!post01.fr7!fx20.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: 1391361524 62.49.20.82 (Sun, 02 Feb 2014 17:18:44 UTC) NNTP-Posting-Date: Sun, 02 Feb 2014 17:18:44 UTC Date: Sun, 02 Feb 2014 17:18:44 GMT X-Received-Body-CRC: 3655689622 X-Received-Bytes: 3691 Xref: number.nntp.dca.giganews.com comp.lang.ada:184635 Date: 2014-02-02T17:18:44+00:00 List-Id: On Sat, 01 Feb 2014 21:47:15 -0500, agent wrote: > On Tue, 28 Jan 2014 12:18:33 GMT, Brian Drummond > 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