comp.lang.ada
 help / color / mirror / Atom feed
* parsing a string
@ 2000-01-15  0:00 Paolo M. Pumilia
  2000-01-15  0:00 ` James S. Rogers
  0 siblings, 1 reply; 12+ messages in thread
From: Paolo M. Pumilia @ 2000-01-15  0:00 UTC (permalink / raw)


I am rather new to Ada programming.  A hint would be useful to  quickly
translate a procedure
that i am used to use in fortran programs.
How to split a string (or unbounded string) into a character array,
using blanks as separators?

thank you for your help

Pol






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

* Re: parsing a string
  2000-01-15  0:00 parsing a string Paolo M. Pumilia
@ 2000-01-15  0:00 ` James S. Rogers
  2000-01-24  0:00   ` pumilia
  2000-01-24  0:00   ` pumilia
  0 siblings, 2 replies; 12+ messages in thread
From: James S. Rogers @ 2000-01-15  0:00 UTC (permalink / raw)


Paolo M. Pumilia wrote in message <388095E5.6BF46237@cstc.org>...
>I am rather new to Ada programming.  A hint would be useful to  quickly
>translate a procedure
>that i am used to use in fortran programs.
>How to split a string (or unbounded string) into a character array,
>using blanks as separators?


There are a lot of ways to do this.  The easiest is to use the package
Ada.Strings.Fixed. For this question you will want to use the
Index function. It will return the index of the first occurance of the
pattern you want to find.

Simply use string slices after that, repeatedly passing the slices
to the Index function, to parse through the entire original string.

Jim Rogers
Colorado Springs, Colorado






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

* Re: parsing a string
  2000-01-15  0:00 ` James S. Rogers
  2000-01-24  0:00   ` pumilia
@ 2000-01-24  0:00   ` pumilia
  1 sibling, 0 replies; 12+ messages in thread
From: pumilia @ 2000-01-24  0:00 UTC (permalink / raw)


In article <85q63n$ese$1@bgtnsc01.worldnet.att.net>,
  "James S. Rogers" <jimmaureenrogers@worldnet.att.net> wrote:
> Paolo M. Pumilia wrote in message <388095E5.6BF46237@cstc.org>...
> >I am rather new to Ada programming.  A hint would be useful to
quickly
> >translate a procedure
> >that i am used to use in fortran programs.
> >How to split a string (or unbounded string) into a character array,
> >using blanks as separators?
>
> There are a lot of ways to do this.  The easiest is to use the package
> Ada.Strings.Fixed. For this question you will want to use the
> Index function. It will return the index of the first occurance of the
> pattern you want to find.
>
> Simply use string slices after that, repeatedly passing the slices
> to the Index function, to parse through the entire original string.
>
> Jim Rogers
> Colorado Springs, Colorado
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: parsing a string
  2000-01-15  0:00 ` James S. Rogers
@ 2000-01-24  0:00   ` pumilia
  2000-01-24  0:00     ` Stephen Leake
  2000-01-24  0:00     ` parsing a string Ehud Lamm
  2000-01-24  0:00   ` pumilia
  1 sibling, 2 replies; 12+ messages in thread
From: pumilia @ 2000-01-24  0:00 UTC (permalink / raw)


In article <85q63n$ese$1@bgtnsc01.worldnet.att.net>,
  "James S. Rogers" <jimmaureenrogers@worldnet.att.net> wrote:
> Paolo M. Pumilia wrote in message <388095E5.6BF46237@cstc.org>...
> >I am rather new to Ada programming.  A hint would be useful to
quickly
> >translate a procedure
> >that i am used to use in fortran programs.
> >How to split a string (or unbounded string) into a character array,
> >using blanks as separators?
>
> There are a lot of ways to do this.  The easiest is to use the package
> Ada.Strings.Fixed. For this question you will want to use the
> Index function. It will return the index of the first occurance of the
> pattern you want to find.
>

Thank you for help. i have found the function Index, defined as follows

     function Index (Source   : in String;
                   Pattern  : in String;
                   Going    : in Direction := Forward;
                   Mapping  : in Maps.Character_Mapping
                                := Maps.Identity)
      return Natural;

in package Ada.Strings.Fixed

I guessed that the last two fields are set by default, so that it is
possible not to pass them any value.
Maybe it is a wrong guess, but i do not know what value can be
passed for  Maps.Character_Mapping

Here is a selection from my test procedure 'dimer.adb', where the
string output_file has been assigned by another procedure:

    with ada.strings.unbounded;
    use ada.strings.unbounded;
    with ada.strings.fixed; use ada.strings.fixed;
     ...
     output_file :  unbounded_string ;
     part1_output_file :  string (1..25) ;
     ...
     output_file :=  to_string(output_file);
     Index(part1_output_file,"put");
     ...

Here is the output from the GNAT compiler:
   % gnatgcc -c -O3 dimer.adb
   > dimer.adb:143:04: invalid parameter list in call (use -gnatf for
details)
   > gnatmake: "dimer.adb" compilation error


   % gnatgcc -c -O3 -gnatf dimer.adb
   > dimer.adb:143:04: no candidate interpretations match the actuals:
   > dimer.adb:143:04: missing argument for parameter "Mapping" in call
to "index" declared at a-strfix.ads:68
   > dimer.adb:143:04: missing argument for parameter "Mapping" in call
to "index" declared at a-strunb.ads:186

I can try the statement

    Index(total_output_file,"put", forward);

but i do not know what to specify as "Mapping"

Thank you for your help

Paolo Pumilia




Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: parsing a string
  2000-01-24  0:00   ` pumilia
  2000-01-24  0:00     ` Stephen Leake
@ 2000-01-24  0:00     ` Ehud Lamm
  1 sibling, 0 replies; 12+ messages in thread
From: Ehud Lamm @ 2000-01-24  0:00 UTC (permalink / raw)


It shouldn't add much to what you already know, but my Ada page (URL
of home page below) has a short intro to Ada string facilites. It also
links to online specs of the relevant packages (Maps etc.) which should
also help. check out the To_Set function.

Ehud Lamm mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <== My home on the web 
Check it out and subscribe to the E-List- for interesting essays and more!






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

* Re: parsing a string
  2000-01-24  0:00   ` pumilia
@ 2000-01-24  0:00     ` Stephen Leake
  2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
  2000-01-24  0:00     ` parsing a string Ehud Lamm
  1 sibling, 1 reply; 12+ messages in thread
From: Stephen Leake @ 2000-01-24  0:00 UTC (permalink / raw)


pumilia@est.it writes:

> In article <85q63n$ese$1@bgtnsc01.worldnet.att.net>,

> Thank you for help. i have found the function Index, defined as follows
> 
>      function Index (Source   : in String;
>                    Pattern  : in String;
>                    Going    : in Direction := Forward;
>                    Mapping  : in Maps.Character_Mapping
>                                 := Maps.Identity)
>       return Natural;
> 
> in package Ada.Strings.Fixed
> 
> I guessed that the last two fields are set by default, so that it is
> possible not to pass them any value.
> Maybe it is a wrong guess, but i do not know what value can be
> passed for  Maps.Character_Mapping
> 
> Here is a selection from my test procedure 'dimer.adb', where the
> string output_file has been assigned by another procedure:
> 
>     with ada.strings.unbounded;
>     use ada.strings.unbounded;
>     with ada.strings.fixed; use ada.strings.fixed;
>      ...
>      output_file :  unbounded_string ;
>      part1_output_file :  string (1..25) ;
>      ...
>      output_file :=  to_string(output_file);
>      Index(part1_output_file,"put");
>      ...

Index is a function, which must be assigned to a value, so this can't
be the real source. Please include your _complete_ test program, so we
can compile it ourselves, to help get a good answer. 

The default for Map should be fine. You can find out what else you
might pass for Map in the package Ada.Strings.Maps.

-- Stephe




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

* Re: CONTRAINT ERROR (was Re: parsing a string)
  2000-01-26  0:00           ` Fraser
@ 2000-01-26  0:00             ` Simon Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Simon Wright @ 2000-01-26  0:00 UTC (permalink / raw)


blancolioni@blancolioni.org (Fraser) writes:

> paene lacrimavi postquam rracine@myremarq.com (Roger Racine) scripsit:
> 
> >The unbounded string Output_File_String can be of any length.  The
> >string Output_File is exactly 25 characters.  The following will help:
> 
> That still limits the length of the string.  I'd be more inclined to
> this:
> 
>      Get_Line (Output_File_String);
> 
>      declare
>         Output_File : String := To_String (Output_File_String);

Or even
        Output_File : constant String := To_String (Output_File_String);




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

* CONTRAINT ERROR (was Re: parsing a string)
  2000-01-24  0:00     ` Stephen Leake
@ 2000-01-26  0:00       ` pumilia
  2000-01-26  0:00         ` Ian Clifton
                           ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: pumilia @ 2000-01-26  0:00 UTC (permalink / raw)


In article <uu2k3kszb.fsf@gsfc.nasa.gov>,
  Stephen Leake <Stephen.Leake@gsfc.nasa.gov> wrote:

> Index is a function, which must be assigned to a value, so this can't
> be the real source. Please include your _complete_ test program, so we
> can compile it ourselves, to help get a good answer.
>
> The default for Map should be fine. You can find out what else you
> might pass for Map in the package Ada.Strings.Maps.
>
> -- Stephe
>

Ok, i simplified my program, adjusted something thanks to David Hoos's
and
your useful comments and found a new error: CONSTRAINT_ERROR.

Here is my test procedure, aimed to converting an unbounded string
to string and then to parse the string:

-- str2_test --------------------------------------
with   ada.strings.unbounded;
use    ada.strings.unbounded;
with    ustrings;
use      ustrings;
with   text_io;
with   Ada.Integer_Text_IO;
use    text_io;
with ada.strings.fixed; use ada.strings.fixed;

with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure str2_test is

 output_file_string : unbounded_string ;
 output_file : string (1..25) ;
 pos1 : natural;

Begin

    get_line(output_file_string);

   put(" output:  "); put(output_file_string); put("<--"); new_line;
   output_file :=  to_string(output_file_string);
   put(" Output:  "); put(output_file); put("<--"); new_line;
   put(" index:  "); pos1 := Index(output_file,"xxx");
   put(pos1);
   new_line;

end str2_test;
-- str2_test --------------------------------------


While executing i get the error:

>   raised constraint_error : str2_test.adb:24

This is line 24:

  output_file :=  to_string(output_file_string);


Any help wil be appreciiated
thank you
Pol

- ustrings.ads ---------------------------------
-- Ideally this would be a child package of "Ada.Strings.Unbounded".

-- package Ustrings is
--   procedure Get_Line(Item : out Unbounded_String);
--   procedure Put(Item : in Unbounded_String);
--   procedure Put_Line(Item : in Unbounded_String);
-- end Ustrings;
--
-- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
-- Author: David A. Wheeler
--

with Text_IO, Ada.Strings.Unbounded;
use  Text_IO, Ada.Strings.Unbounded;

package Ustrings is

  -- This package provides a simpler way to work with type
  -- Unbounded_String, since this type will be used very often.
  -- Most users will want to ALSO with "Ada.Strings.Unbounded".
  -- Ideally this would be a child package of "Ada.Strings.Unbounded".
  --

  -- This package provides the following simplifications:
  --  + Shortens the type name from "Unbounded_String" to "Ustring".
  --  + Creates shorter function names for To_Unbounded_String, i.e.
  --    To_Ustring(U) and U(S).  "U" is not a very readable name, but
  --    it's such a common operation that a short name seems appropriate
  --    (this function is needed every time a String constant is used).
  --    It also creates S(U) as the reverse of U(S).
  --  + Adds other subprograms, currently just "Swap".
  --  + Other packages can use this package to provide other
simplifications.

  subtype Ustring is Unbounded_String;

  function To_Ustring(Source : String)  return Unbounded_String
                                         renames To_Unbounded_String;
  function U(Source : String)           return Unbounded_String
                                         renames To_Unbounded_String;
  function S(Source : Unbounded_String) return String
                                         renames To_String;

  -- "Swap" is important for reuse in some other packages, so we'll
define it.

  procedure Swap(Left, Right : in out Unbounded_String);


  function Empty(S : Unbounded_String) return Boolean;
   -- returns True if Length(S)=0.
  pragma Inline(Empty);


  -- I/O Routines.
  procedure Get_Line(File : in File_Type; Item : out Unbounded_String);
  procedure Get_Line(Item : out Unbounded_String);

  procedure Put(File : in File_Type; Item : in Unbounded_String);
  procedure Put(Item : in Unbounded_String);

  procedure Put_Line(File : in File_Type; Item : in Unbounded_String);
  procedure Put_Line(Item : in Unbounded_String);

end Ustrings;

- ustrings.ads ---------------------------------



Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: CONTRAINT ERROR (was Re: parsing a string)
  2000-01-26  0:00         ` Roger Racine
@ 2000-01-26  0:00           ` Fraser
  2000-01-26  0:00             ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Fraser @ 2000-01-26  0:00 UTC (permalink / raw)


paene lacrimavi postquam rracine@myremarq.com (Roger Racine) scripsit:

>The unbounded string Output_File_String can be of any length.  The
>string Output_File is exactly 25 characters.  The following will help:

That still limits the length of the string.  I'd be more inclined to
this:

     Get_Line (Output_File_String);

     declare
        Output_File : String := To_String (Output_File_String);
     begin
        ...
     end;


I just hate padded strings.  When something is a String, 'Length,
'First, 'Last and 'Range should all be meaningful.  I reckon.

Fraser.




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

* Re: CONTRAINT ERROR (was Re: parsing a string)
  2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
  2000-01-26  0:00         ` Ian Clifton
  2000-01-26  0:00         ` John English
@ 2000-01-26  0:00         ` Roger Racine
  2000-01-26  0:00           ` Fraser
  2 siblings, 1 reply; 12+ messages in thread
From: Roger Racine @ 2000-01-26  0:00 UTC (permalink / raw)


On Wed, 26 Jan 2000 11:33:44 GMT, pumilia@est.it wrote:

>In article <uu2k3kszb.fsf@gsfc.nasa.gov>,
>  Stephen Leake <Stephen.Leake@gsfc.nasa.gov> wrote:
>
>> Index is a function, which must be assigned to a value, so this can't
>> be the real source. Please include your _complete_ test program, so we
>> can compile it ourselves, to help get a good answer.
>>
>> The default for Map should be fine. You can find out what else you
>> might pass for Map in the package Ada.Strings.Maps.
>>
>> -- Stephe
>>
>
>Ok, i simplified my program, adjusted something thanks to David Hoos's
>and
>your useful comments and found a new error: CONSTRAINT_ERROR.
>
>Here is my test procedure, aimed to converting an unbounded string
>to string and then to parse the string:
>
>-- str2_test --------------------------------------
>with   ada.strings.unbounded;
>use    ada.strings.unbounded;
>with    ustrings;
>use      ustrings;
>with   text_io;
>with   Ada.Integer_Text_IO;
>use    text_io;
>with ada.strings.fixed; use ada.strings.fixed;
>
>with Ada.Integer_Text_IO;
>use Ada.Integer_Text_IO;
>
>procedure str2_test is
>
> output_file_string : unbounded_string ;
> output_file : string (1..25) ;
> pos1 : natural;
>
>Begin
>
>    get_line(output_file_string);
>
>   put(" output:  "); put(output_file_string); put("<--"); new_line;
>   output_file :=  to_string(output_file_string);
>   put(" Output:  "); put(output_file); put("<--"); new_line;
>   put(" index:  "); pos1 := Index(output_file,"xxx");
>   put(pos1);
>   new_line;
>
>end str2_test;
>-- str2_test --------------------------------------
>
>
>While executing i get the error:
>
>>   raised constraint_error : str2_test.adb:24
>
>This is line 24:
>
>  output_file :=  to_string(output_file_string);
>
>
>Any help wil be appreciiated
>thank you
>Pol
>
>- ustrings.ads ---------------------------------
>-- Ideally this would be a child package of "Ada.Strings.Unbounded".
>
>-- package Ustrings is
>--   procedure Get_Line(Item : out Unbounded_String);
>--   procedure Put(Item : in Unbounded_String);
>--   procedure Put_Line(Item : in Unbounded_String);
>-- end Ustrings;
>--
>-- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
>-- Author: David A. Wheeler
>--
>

The unbounded string Output_File_String can be of any length.  The
string Output_File is exactly 25 characters.  The following will help:

  Ulength : Natural;
...
   ULength := Length (Output_File_String);
   if ULength > Output_file'Length then
-- Do something to make sure bad input is caught

      raise Constraint_Error;  

   else

   Output_File(1 .. ULength) :=  to_string(output_file_string);
...
end if;

Roger Racine





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

* Re: CONTRAINT ERROR (was Re: parsing a string)
  2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
@ 2000-01-26  0:00         ` Ian Clifton
  2000-01-26  0:00         ` John English
  2000-01-26  0:00         ` Roger Racine
  2 siblings, 0 replies; 12+ messages in thread
From: Ian Clifton @ 2000-01-26  0:00 UTC (permalink / raw)


pumilia@est.it writes:

>  output_file_string : unbounded_string ;
>  output_file : string (1..25) ;
>  pos1 : natural;
> 
> Begin
> 
>     get_line(output_file_string);
> 
>    put(" output:  "); put(output_file_string); put("<--"); new_line;
>    output_file :=  to_string(output_file_string);
>    put(" Output:  "); put(output_file); put("<--"); new_line;
>    put(" index:  "); pos1 := Index(output_file,"xxx");
>    put(pos1);
>    new_line;
> 
> end str2_test;
> -- str2_test --------------------------------------
> 
> 
> While executing i get the error:
> 
> >   raised constraint_error : str2_test.adb:24
> 
> This is line 24:
> 
>   output_file :=  to_string(output_file_string);
> 
> 
> Any help wil be appreciiated
> thank you
> Pol

I think the problem is that output_file is a fixed-length(=25) string,
while Ada.Strings.Unbounded.To_String returns a fixed string of
whatever length output_file_string happens to be. This will fail
unless that length happens to be 25. In places where you'd like to
assign to a fixed-length string, it almost always makes more sense to
use Ada.Strings.Fixed.Move instead. Ada.Strings.Fixed.Move pads or
truncates appropriately to match the length of the target string.

-- 
Ian Clifton                   Phone: +44 1865 275631
Dyson Perrins Laboratory      Fax:   +44 1865 275674
Oxford University  OX1 3QY UK ian.clifton@linacre.ox.ac.uk




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

* Re: CONTRAINT ERROR (was Re: parsing a string)
  2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
  2000-01-26  0:00         ` Ian Clifton
@ 2000-01-26  0:00         ` John English
  2000-01-26  0:00         ` Roger Racine
  2 siblings, 0 replies; 12+ messages in thread
From: John English @ 2000-01-26  0:00 UTC (permalink / raw)


pumilia@est.it wrote:
> Here is my test procedure, aimed to converting an unbounded string
> to string and then to parse the string:
> 
> -- str2_test --------------------------------------
> [...snip...]
> procedure str2_test is
> 
>  output_file_string : unbounded_string ;
>  output_file : string (1..25) ;
>  pos1 : natural;
> 
> Begin
> 
>     get_line(output_file_string);
> 
>    put(" output:  "); put(output_file_string); put("<--"); new_line;
>    output_file :=  to_string(output_file_string);

Output_String is exactly 25 characters long, so Output_File_String
has to be exactly 25 characters long too. If it isn't, you'll get
a Constraint_Error, just as you discovered.

Try this:
  if Length(Output_File_String) > 25 then
    Output_File := To_String(Output_File_Length)(1..25);
  else
    Output_File := (others => ' ');  -- to pad result with spaces
    Output_File(1..Length(Output_File_String)) :=
                   To_String(Output_File_Length);
  end if;

(untested, and would be better with the aid of Output_File'Length,
Output_File'First and Output_File'Last instead of magic numbers 1
and 25.)

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

end of thread, other threads:[~2000-01-26  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-15  0:00 parsing a string Paolo M. Pumilia
2000-01-15  0:00 ` James S. Rogers
2000-01-24  0:00   ` pumilia
2000-01-24  0:00     ` Stephen Leake
2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
2000-01-26  0:00         ` Ian Clifton
2000-01-26  0:00         ` John English
2000-01-26  0:00         ` Roger Racine
2000-01-26  0:00           ` Fraser
2000-01-26  0:00             ` Simon Wright
2000-01-24  0:00     ` parsing a string Ehud Lamm
2000-01-24  0:00   ` pumilia

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