comp.lang.ada
 help / color / mirror / Atom feed
* Problems with String processing
@ 2010-10-23 20:16 George
  2010-10-23 20:24 ` Niklas Holsti
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: George @ 2010-10-23 20:16 UTC (permalink / raw)


Hi All,

I have some problems with string processing. My program looks like this:

begin
-- Ada.Command_Line.Argument_Count is "0" if
-- a) Command_Line.Argument_Count is not implemented (Compiler, OS)
-- b) no arguments given
if Ada.Command_Line.Argument_Count > 0 then
   for counter in 1..Ada.Command_Line.Argument_Count loop
      Arg = Ada.Characters.Handling.To_Upper(Ada.Command_Line.Argument
(counter)); -- this assignment is the problem
      if Arg = "ADA83" then
         Ada83;
      elsif Arg = "ADA95" then
         Ada95;
      elsif Arg = "ADA2005" then
         Ada2005;
      elsif Arg = "ATTRIBUTES" then
         Attributes;
      elsif Arg = "ALL" then
         Ada83;
         Ada95;
         Ada2005;
         Attributes;
         Sources;
         Author;
      else -- unknown argument
         Ada.Text_IO.put_line("Given Argument is unknown!");
      end if;
   end loop;

The assignment Arg = Ada.Characters.Handling.To_Upper
(Ada.Command_Line.Argument(counter)); will not work due to a CONSTRAINT 
ERROR. The problem is that the command line arguments given are of 
different length. Ada expects the string to match exactly the length of 
the defined variable "Arg" with 10 characters.

How could I solve that problem?

Regards

George



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems with String processing
  2010-10-23 20:16 Problems with String processing George
@ 2010-10-23 20:24 ` Niklas Holsti
  2010-10-23 20:25 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Niklas Holsti @ 2010-10-23 20:24 UTC (permalink / raw)


George wrote:
> Hi All,
> 
> I have some problems with string processing. My program looks like this:
> 
> begin
> -- Ada.Command_Line.Argument_Count is "0" if
> -- a) Command_Line.Argument_Count is not implemented (Compiler, OS)
> -- b) no arguments given
> if Ada.Command_Line.Argument_Count > 0 then
>    for counter in 1..Ada.Command_Line.Argument_Count loop
>       Arg = Ada.Characters.Handling.To_Upper(Ada.Command_Line.Argument
> (counter)); -- this assignment is the problem

Firstly, that isn't an assignment, since you wrote "=". You should write 
":=" for an assignment.

Secondly, as you have now learned, the Ada String type is a fixed-length 
string. The simplest solution is to declare the Arg variable as a 
constant String that is given its value (and length) in the declaration 
itself:

    declare
       Arg : constant String := Ada.Characters.Handling.To_Upper (
          Ada.Command_Line.Argument (counter));
    begin

>       if Arg = "ADA83" then
>          Ada83;
>       elsif Arg = "ADA95" then
>          Ada95;
>       elsif Arg = "ADA2005" then
>          Ada2005;
>       elsif Arg = "ATTRIBUTES" then
>          Attributes;
>       elsif Arg = "ALL" then
>          Ada83;
>          Ada95;
>          Ada2005;
>          Attributes;
>          Sources;
>          Author;
>       else -- unknown argument
>          Ada.Text_IO.put_line("Given Argument is unknown!");
>       end if;

    end;

>    end loop;
> 
> The assignment Arg = Ada.Characters.Handling.To_Upper
> (Ada.Command_Line.Argument(counter)); will not work due to a CONSTRAINT 
> ERROR. The problem is that the command line arguments given are of 
> different length. Ada expects the string to match exactly the length of 
> the defined variable "Arg" with 10 characters.
> 
> How could I solve that problem?

Another solution is to use Ada.Strings.Unbounded.Unbounded_String, which 
is the main Ada type for variable-length strings.

HTH,

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems with String processing
  2010-10-23 20:16 Problems with String processing George
  2010-10-23 20:24 ` Niklas Holsti
@ 2010-10-23 20:25 ` Dmitry A. Kazakov
  2010-10-23 20:43 ` George
  2010-10-23 23:59 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2010-10-23 20:25 UTC (permalink / raw)


On 23 Oct 2010 20:16:53 GMT, George wrote:

> begin
> -- Ada.Command_Line.Argument_Count is "0" if
> -- a) Command_Line.Argument_Count is not implemented (Compiler, OS)
> -- b) no arguments given
> if Ada.Command_Line.Argument_Count > 0 then
>    for counter in 1..Ada.Command_Line.Argument_Count loop

declare
   Arg : constant String :=  Ada.Characters.Handling.
       To_Upper (Ada.Command_Line.Argument (Counter)); 
begin
>       if Arg = "ADA83" then
>          Ada83;
>       elsif Arg = "ADA95" then
>          Ada95;
>       elsif Arg = "ADA2005" then
>          Ada2005;
>       elsif Arg = "ATTRIBUTES" then
>          Attributes;
>       elsif Arg = "ALL" then
>          Ada83;
>          Ada95;
>          Ada2005;
>          Attributes;
>          Sources;
>          Author;
>       else -- unknown argument
>          Ada.Text_IO.put_line("Given Argument is unknown!");
>       end if;

end;

>    end loop;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems with String processing
  2010-10-23 20:16 Problems with String processing George
  2010-10-23 20:24 ` Niklas Holsti
  2010-10-23 20:25 ` Dmitry A. Kazakov
@ 2010-10-23 20:43 ` George
  2010-10-23 23:59 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: George @ 2010-10-23 20:43 UTC (permalink / raw)


Hi Niklas,
Hi Dimitry,

many thanks for your help.

I thought about declaring Arg on the fly but did not know how to do it. 
You showed me. Thanks again.

Best regards

George





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems with String processing
  2010-10-23 20:16 Problems with String processing George
                   ` (2 preceding siblings ...)
  2010-10-23 20:43 ` George
@ 2010-10-23 23:59 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2010-10-23 23:59 UTC (permalink / raw)


On 10/23/2010 01:16 PM, George wrote:
>
> begin
> -- Ada.Command_Line.Argument_Count is "0" if
> -- a) Command_Line.Argument_Count is not implemented (Compiler, OS)
> -- b) no arguments given
> if Ada.Command_Line.Argument_Count>  0 then
>     for counter in 1..Ada.Command_Line.Argument_Count loop

Unrelated to your question, which others have answered, this "if" is not needed. 
The loop will execute zero times if Argument_Count < 1.

-- 
Jeff Carter
"Unix and C are the ultimate computer viruses."
Richard Gabriel
99



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-10-23 23:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-23 20:16 Problems with String processing George
2010-10-23 20:24 ` Niklas Holsti
2010-10-23 20:25 ` Dmitry A. Kazakov
2010-10-23 20:43 ` George
2010-10-23 23:59 ` Jeffrey Carter

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