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.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f391a16b66b79f94 X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: raised CONSTRAINT_ERROR Date: 2000/07/24 Message-ID: <8lhldq$ock$1@nnrp1.deja.com>#1/1 X-Deja-AN: 650138148 References: <86lmytp5i0.fsf@book.mteege.de> X-Http-Proxy: 1.0 x51.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Mon Jul 24 14:55:57 2000 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.7 [en] (WinNT; I) Date: 2000-07-24T00:00:00+00:00 List-Id: In article <86lmytp5i0.fsf@book.mteege.de>, Matthias Teege wrote: > now I have a problem that I didnt understand. The followig > code gives me a raised CONSTRAINT_ERROR (in Line: > StrArg := Ada.Command_Line.Argument(I)) and I didnt know > why. ... > StrArg : String (1 .. 10); ... > StrArg := Ada.Command_Line.Argument(I); That's why. Unless your command line argument happens to be *exactly* 10 characters long, you will get a constraint error here. Check out the following entry in our FAQ: http://www.adapower.com/lab/adafaq/24.html Ada's string handling is very powerful. But its also very different from most other languages. The important thing to remember is that its length is defined by the variable's *declaration*, not by its contents. Generally you either want to set the string's size with an initialization: loop declare StrArg : constant String := Ada.Command_Line.Argument(I); begin ... end; end loop; ...or you want to declare one string big enough to hold the entire argument, no matter how big it could reasonably get, then assign into a correctly-sized slice of it. Str_Arg_Length := Ada.Command_Line.Argument(I)'length; StrArg (1..Str_Arg_Length) := Ada.Command_Line.Argument(I); Put_Line (StrArg(1..Str_Arg_Length)); Since Ada strings don't rely on a terminator to define their length, the 'length operation does *not* iterate through the string. It might not even generate any code at all if you have optimizations on. Another alternative is to use a dynamic string type, like Ada.Strings.Unbounded.Unbounded_String. Or you can use the source string in-situ (like you did in your second example) -- T.E.D. http://www.telepath.com/~dennison/Ted/TED.html Sent via Deja.com http://www.deja.com/ Before you buy.