comp.lang.ada
 help / color / mirror / Atom feed
* String manupulation
@ 2001-08-21  7:27 Reinert Korsnes
  2001-08-21 10:52 ` Georg Bauhaus
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Reinert Korsnes @ 2001-08-21  7:27 UTC (permalink / raw)


Hi,

I just wondered if there is a simple/direct way (in Ada) to split
a string with words (for example "abc    defg  2.56  hijklm") 
into stings each containing one word ("abc", "defg", "2.56" and "hijklm") ?

Something like the functions of Ada.Command_Line ?

Yes, I can program this myself, but maybe I should use something 
"standard" ? 

reinert

-- 
http://home.chello.no/~rkorsnes



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

* Re: String manupulation
  2001-08-21  7:27 String manupulation Reinert Korsnes
@ 2001-08-21 10:52 ` Georg Bauhaus
  2001-08-21 12:52 ` David C. Hoos
  2001-08-21 18:04 ` Randy Brukardt
  2 siblings, 0 replies; 12+ messages in thread
From: Georg Bauhaus @ 2001-08-21 10:52 UTC (permalink / raw)


Reinert Korsnes <Reinert.Korsnes@ffi.no> wrote:
 
: a string with words (for example "abc    defg  2.56  hijklm") 
: into stings each containing one word ("abc", "defg", "2.56" and "hijklm") ?

parhaps Index_Non_Blank from A.4.3 and relatives?

Georg



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

* Re: String manupulation
  2001-08-21  7:27 String manupulation Reinert Korsnes
  2001-08-21 10:52 ` Georg Bauhaus
@ 2001-08-21 12:52 ` David C. Hoos
  2001-08-21 18:04 ` Randy Brukardt
  2 siblings, 0 replies; 12+ messages in thread
From: David C. Hoos @ 2001-08-21 12:52 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Reinert.Korsnes

This is a job for Ada.Strings.Fixed.Find_Token

You would specify the whitespace characters as the
parameter "set," and "outside" as the membership test.

----- Original Message -----
From: "Reinert Korsnes" <Reinert.Korsnes@ffi.no>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Tuesday, August 21, 2001 2:27 AM
Subject: String manupulation


> Hi,
>
> I just wondered if there is a simple/direct way (in Ada) to split
> a string with words (for example "abc    defg  2.56  hijklm")
> into stings each containing one word ("abc", "defg", "2.56" and "hijklm")
?
>
> Something like the functions of Ada.Command_Line ?
>
> Yes, I can program this myself, but maybe I should use something
> "standard" ?
>
> reinert
>
> --
> http://home.chello.no/~rkorsnes
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




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

* Re: String manupulation
  2001-08-21  7:27 String manupulation Reinert Korsnes
  2001-08-21 10:52 ` Georg Bauhaus
  2001-08-21 12:52 ` David C. Hoos
@ 2001-08-21 18:04 ` Randy Brukardt
  2001-08-22  8:15   ` Reinert Korsnes
  2 siblings, 1 reply; 12+ messages in thread
From: Randy Brukardt @ 2001-08-21 18:04 UTC (permalink / raw)


Reinert Korsnes wrote in message <9lt2pe$lrm$1@snipp.uninett.no>...
>Hi,
>
>I just wondered if there is a simple/direct way (in Ada) to split
>a string with words (for example "abc    defg  2.56  hijklm")
>into stings each containing one word ("abc", "defg", "2.56" and
"hijklm") ?

Look at Ada.Strings.Fixed.Find_Token (see RM A.4.3(68):
http://www.adaic.com/standards/95lrm/html/RM-A-4-3.html)

There are versions of this in Ada.Strings.Bounded and
Ada.Strings.Unbounded as well.

                Randy Brukardt.







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

* Re: String manupulation
  2001-08-21 18:04 ` Randy Brukardt
@ 2001-08-22  8:15   ` Reinert Korsnes
  2001-08-22 15:53     ` Ted Dennison
  0 siblings, 1 reply; 12+ messages in thread
From: Reinert Korsnes @ 2001-08-22  8:15 UTC (permalink / raw)


Thanks to all of you.  Is this the (dirty ?) way to do it (?) :

-----------------------------------------------
with Text_IO,Ada.Strings,Ada.Strings.Fixed,Ada.Strings.Maps;
use  Text_IO,Ada.Strings,Ada.Strings.Fixed,Ada.Strings.Maps;
procedure Rtest1 is
   package Int_Io is new Text_IO.Integer_Io (Integer);
   use Int_Io;
   I,J : Integer := 1;
   FS : String := " This is a test to split a string ";
   C1 : String := "123456789012345678901234";
   Set : Character_Set := To_Set(" ");
begin
   Put(C1);New_Line;
   Put(FS);New_Line;
   while I < FS'Length loop
      Find_Token(FS(I..FS'Length),Set,Outside,I,J);
      Put(I);Put(J); Put(" ");Put(FS(I..J));New_Line;
      I := J+1;
   end loop;
end Rtest1;
-----------------------------------------------

Randy Brukardt wrote:

> Reinert Korsnes wrote in message <9lt2pe$lrm$1@snipp.uninett.no>...
>>Hi,
>>
>>I just wondered if there is a simple/direct way (in Ada) to split
>>a string with words (for example "abc    defg  2.56  hijklm")
>>into stings each containing one word ("abc", "defg", "2.56" and
> "hijklm") ?
> 
> Look at Ada.Strings.Fixed.Find_Token (see RM A.4.3(68):
> http://www.adaic.com/standards/95lrm/html/RM-A-4-3.html)
> 
> There are versions of this in Ada.Strings.Bounded and
> Ada.Strings.Unbounded as well.
> 
>                 Randy Brukardt.
> 
> 
> 
> 
> 

-- 
http://home.chello.no/~rkorsnes



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

* Re: String manupulation
  2001-08-22  8:15   ` Reinert Korsnes
@ 2001-08-22 15:53     ` Ted Dennison
  2001-08-22 16:09       ` Ted Dennison
                         ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Ted Dennison @ 2001-08-22 15:53 UTC (permalink / raw)


In article <9lvq0p$cpm$1@snipp.uninett.no>, Reinert Korsnes says...
>
>Thanks to all of you.  Is this the (dirty ?) way to do it (?) :

Some style comments:

o  Anything that never changes after the declaration should be declared
"constant".

o  There's already an instantiation of Integer_IO for Integer. Its
Ada.Integer_Text_IO.

o Unless it won't work for some reason, I generally prefer to use 'Image rather
than a numeric Text_IO instantiation.

o You don't need to "with" a package if you already "with" one of its children.

o Except in extreme quick-n-dirty code, I always specify the parameter names in
multi-parameter subprogram calls.

o (Contraversial) I prefer to aviod the "use" clause.

o There's no indication where the "1" you initialize those variables to comes
from or why its there. Instead, only initialize the one(s) you need to, and do
it from the array it is using to index through.

Also, while transforming that appropriately, I noticed that you have a bug where
it doesn't handle a final substring of length 1 properly. That's the kind of
thing you are more apt to notice the more closely you tie your indices to your
array. 

Also, Find_Token returns 0 for Last when no more spaces are found. If you supply
a string that doesn't end in a space, your code will loop endlessly. The
*proper* way to terminate your "Find_Token" based loop is to terminate when no
further token is found (Last = 0).

Given all that, I'd transform it to:
------------------------
with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;

procedure Rtest1 is
FS    : constant String := " This is a test to split a string ";
C1    : constant String := "123456789012345678901234";
Space : constant Ada.Strings.Maps.Character_Set := Ada.Strings.Maps.To_Set(" ");

First : Natural := Fs'First;
Last  : Natural;
begin
Ada.Text_IO.Put_Line (C1);
Ada.Text_IO.Put_Line (FS);

loop
Ada.Strings.Fixed.Find_Token
(Source => FS(First..FS'Length),
Set    => Space,
Test   => Ada.Strings.Outside,
First  => First,
Last   => Last
);

exit when Last = 0;

Ada.Text_IO.Put_Line
(Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));

First := Last + 1;
end loop;
end Rtest1;
-------------

To satify those who don't agree with the "no use" bit, I'll also include a
version with "use"s. Obviously *I* don't think it looks better, but some here
surely will:

-------------
with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;

use Ada.Text_IO;
use Ada.Strings.Fixed;
use Ada.Strings.Maps;

procedure Rtest1 is
FS    : constant String := " This is a test to split a string ";
C1    : constant String := "123456789012345678901234";
Space : constant Character_Set := To_Set(" ");

First : Natural := Fs'First;
Last  : Natural;
begin
Put_Line (C1);
Put_Line (FS);

loop
Find_Token
(Source => FS(First..FS'Length),
Set    => Space,
Test   => Ada.Strings.Outside,
First  => First,
Last   => Last
);

exit when Last = 0;

Put_Line
(Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));

First := Last + 1;
end loop;
end Rtest1;
-------------

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: String manupulation
  2001-08-22 15:53     ` Ted Dennison
@ 2001-08-22 16:09       ` Ted Dennison
  2001-08-22 16:23       ` David C. Hoos
  2001-08-22 16:39       ` Warren W. Gay VE3WWG
  2 siblings, 0 replies; 12+ messages in thread
From: Ted Dennison @ 2001-08-22 16:09 UTC (permalink / raw)


In article <0AQg7.10582$2u.75569@www.newsranger.com>, Ted Dennison says...
>begin
>Ada.Text_IO.Put_Line (C1);
>Ada.Text_IO.Put_Line (FS);
>
>loop
>Ada.Strings.Fixed.Find_Token


Urgh. My free posting software likes to remove leading spaces. Please don't take
the lack of formatting as my comment on how things should be indented. You had
that exactly right in your original.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: String manupulation
  2001-08-22 15:53     ` Ted Dennison
  2001-08-22 16:09       ` Ted Dennison
@ 2001-08-22 16:23       ` David C. Hoos
  2001-08-22 16:45         ` Ted Dennison
  2001-08-23  8:21         ` Reinert Korsnes
  2001-08-22 16:39       ` Warren W. Gay VE3WWG
  2 siblings, 2 replies; 12+ messages in thread
From: David C. Hoos @ 2001-08-22 16:23 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Ted Dennison, Reinert.Korsnes

Some additional style comments:

1. One might wish to extend the "Space" object
    to be "Whitespace" with the appropriate set of
    characters, in case one is reading a file with
    tabs or other "whitespace" characters.

2.  The use of a construct like FS'Length would
     better be replaced with FS'Last to cover cases like
     the string being parsed is an input parameter to the
     subprogram, and the caller calls with a slice not
     starting with index 1.

----- Original Message -----
From: "Ted Dennison" <dennison@telepath.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, August 22, 2001 10:53 AM
Subject: Re: String manupulation


> In article <9lvq0p$cpm$1@snipp.uninett.no>, Reinert Korsnes says...
> >
> >Thanks to all of you.  Is this the (dirty ?) way to do it (?) :
>
> Some style comments:
>
> o  Anything that never changes after the declaration should be declared
> "constant".
>
> o  There's already an instantiation of Integer_IO for Integer. Its
> Ada.Integer_Text_IO.
>
> o Unless it won't work for some reason, I generally prefer to use 'Image
rather
> than a numeric Text_IO instantiation.
>
> o You don't need to "with" a package if you already "with" one of its
children.
>
> o Except in extreme quick-n-dirty code, I always specify the parameter
names in
> multi-parameter subprogram calls.
>
> o (Contraversial) I prefer to aviod the "use" clause.
>
> o There's no indication where the "1" you initialize those variables to
comes
> from or why its there. Instead, only initialize the one(s) you need to,
and do
> it from the array it is using to index through.
>
> Also, while transforming that appropriately, I noticed that you have a bug
where
> it doesn't handle a final substring of length 1 properly. That's the kind
of
> thing you are more apt to notice the more closely you tie your indices to
your
> array.
>
> Also, Find_Token returns 0 for Last when no more spaces are found. If you
supply
> a string that doesn't end in a space, your code will loop endlessly. The
> *proper* way to terminate your "Find_Token" based loop is to terminate
when no
> further token is found (Last = 0).
>
> Given all that, I'd transform it to:
> ------------------------
> with Ada.Text_IO;
> with Ada.Strings.Fixed;
> with Ada.Strings.Maps;
>
> procedure Rtest1 is
> FS    : constant String := " This is a test to split a string ";
> C1    : constant String := "123456789012345678901234";
> Space : constant Ada.Strings.Maps.Character_Set :=
Ada.Strings.Maps.To_Set(" ");
>
> First : Natural := Fs'First;
> Last  : Natural;
> begin
> Ada.Text_IO.Put_Line (C1);
> Ada.Text_IO.Put_Line (FS);
>
> loop
> Ada.Strings.Fixed.Find_Token
> (Source => FS(First..FS'Length),
> Set    => Space,
> Test   => Ada.Strings.Outside,
> First  => First,
> Last   => Last
> );
>
> exit when Last = 0;
>
> Ada.Text_IO.Put_Line
> (Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));
>
> First := Last + 1;
> end loop;
> end Rtest1;
> -------------
>
> To satify those who don't agree with the "no use" bit, I'll also include a
> version with "use"s. Obviously *I* don't think it looks better, but some
here
> surely will:
>
> -------------
> with Ada.Text_IO;
> with Ada.Strings.Fixed;
> with Ada.Strings.Maps;
>
> use Ada.Text_IO;
> use Ada.Strings.Fixed;
> use Ada.Strings.Maps;
>
> procedure Rtest1 is
> FS    : constant String := " This is a test to split a string ";
> C1    : constant String := "123456789012345678901234";
> Space : constant Character_Set := To_Set(" ");
>
> First : Natural := Fs'First;
> Last  : Natural;
> begin
> Put_Line (C1);
> Put_Line (FS);
>
> loop
> Find_Token
> (Source => FS(First..FS'Length),
> Set    => Space,
> Test   => Ada.Strings.Outside,
> First  => First,
> Last   => Last
> );
>
> exit when Last = 0;
>
> Put_Line
> (Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));
>
> First := Last + 1;
> end loop;
> end Rtest1;
> -------------
>
> ---
> T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
>           home email - mailto:dennison@telepath.com
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




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

* Re: String manupulation
  2001-08-22 15:53     ` Ted Dennison
  2001-08-22 16:09       ` Ted Dennison
  2001-08-22 16:23       ` David C. Hoos
@ 2001-08-22 16:39       ` Warren W. Gay VE3WWG
  2 siblings, 0 replies; 12+ messages in thread
From: Warren W. Gay VE3WWG @ 2001-08-22 16:39 UTC (permalink / raw)


Ted Dennison wrote:
> In article <9lvq0p$cpm$1@snipp.uninett.no>, Reinert Korsnes says...
> >
> >Thanks to all of you.  Is this the (dirty ?) way to do it (?) :
> 
> Some style comments:
> o (Contraversial) I prefer to aviod the "use" clause.

I agree heartily with this in general.  However, I find that as
a compromise to this, that using a "use" clause in the block of
code where you need it (declare/procedure/function level) can be
most helpful at times, if you don't over do it. For example,
I'll often use:

	declare
		use Ada.Text_IO;
	begin
		...
		Put_Line("...");
	end;

This works reasonably well because the use clause is close
to the code that you are reading. 

Just my $0.02 worth.
-- 
Warren W. Gay VE3WWG
http://members.home.net/ve3wwg



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

* Re: String manupulation
  2001-08-22 16:23       ` David C. Hoos
@ 2001-08-22 16:45         ` Ted Dennison
  2001-08-23  8:21         ` Reinert Korsnes
  1 sibling, 0 replies; 12+ messages in thread
From: Ted Dennison @ 2001-08-22 16:45 UTC (permalink / raw)


In article <mailman.998497411.5812.comp.lang.ada@ada.eu.org>, David C. Hoos
says...
>
>Some additional style comments:
>2.  The use of a construct like FS'Length would
>     better be replaced with FS'Last to cover cases like
>     the string being parsed is an input parameter to the
>     subprogram, and the caller calls with a slice not
>     starting with index 1.

Good catch. I missed that one.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: String manupulation
  2001-08-22 16:23       ` David C. Hoos
  2001-08-22 16:45         ` Ted Dennison
@ 2001-08-23  8:21         ` Reinert Korsnes
  2001-08-23 12:46           ` David C. Hoos
  1 sibling, 1 reply; 12+ messages in thread
From: Reinert Korsnes @ 2001-08-23  8:21 UTC (permalink / raw)


David C. Hoos wrote:

> Some additional style comments:
> 
> 1. One might wish to extend the "Space" object
>     to be "Whitespace" with the appropriate set of
>     characters, in case one is reading a file with
>     tabs or other "whitespace" characters.
> 

How do I define Whitespace ?
I look at Ada.Characters.Latin-1  (find HT etc).
Should I compose my own definition or is there something build-in
and which I have not discovered ?

reinert

...snip...

-- 
http://home.chello.no/~rkorsnes



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

* Re: String manupulation
  2001-08-23  8:21         ` Reinert Korsnes
@ 2001-08-23 12:46           ` David C. Hoos
  0 siblings, 0 replies; 12+ messages in thread
From: David C. Hoos @ 2001-08-23 12:46 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Reinert.Korsnes

I personally use the following definition:

   Whitespace : constant Ada.Strings.Maps.Character_Set :=
     Ada.Strings.Maps.To_Set
     (Ada.Strings.Maps.Character_Range'((Character'Val (0), ' ')));

----- Original Message ----- 
From: "Reinert Korsnes" <Reinert.Korsnes@ffi.no>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, August 23, 2001 3:21 AM
Subject: Re: String manupulation


> David C. Hoos wrote:
> 
> > Some additional style comments:
> > 
> > 1. One might wish to extend the "Space" object
> >     to be "Whitespace" with the appropriate set of
> >     characters, in case one is reading a file with
> >     tabs or other "whitespace" characters.
> > 
> 
> How do I define Whitespace ?
> I look at Ada.Characters.Latin-1  (find HT etc).
> Should I compose my own definition or is there something build-in
> and which I have not discovered ?
> 
> reinert
> 
> ...snip...
> 
> -- 
> http://home.chello.no/~rkorsnes
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

end of thread, other threads:[~2001-08-23 12:46 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-21  7:27 String manupulation Reinert Korsnes
2001-08-21 10:52 ` Georg Bauhaus
2001-08-21 12:52 ` David C. Hoos
2001-08-21 18:04 ` Randy Brukardt
2001-08-22  8:15   ` Reinert Korsnes
2001-08-22 15:53     ` Ted Dennison
2001-08-22 16:09       ` Ted Dennison
2001-08-22 16:23       ` David C. Hoos
2001-08-22 16:45         ` Ted Dennison
2001-08-23  8:21         ` Reinert Korsnes
2001-08-23 12:46           ` David C. Hoos
2001-08-22 16:39       ` Warren W. Gay VE3WWG

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