comp.lang.ada
 help / color / mirror / Atom feed
* how to parse words from a string
@ 2002-11-12 19:33 Sarah Thomas
  2002-11-12 20:15 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Sarah Thomas @ 2002-11-12 19:33 UTC (permalink / raw)


i'm new to ada, could someone help me with the following:
i need to read in an input file and parse each line word by word.
I read in each line of text, but now i need to extract the words
(separated by tabs) into an array, how do i do that...would appreciate
some suggestions, thanks



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

* Re: how to parse words from a string
  2002-11-12 19:33 how to parse words from a string Sarah Thomas
@ 2002-11-12 20:15 ` Stephen Leake
  2002-11-12 20:16 ` Simon Wright
  2002-11-13 10:17 ` Caffeine Junky
  2 siblings, 0 replies; 15+ messages in thread
From: Stephen Leake @ 2002-11-12 20:15 UTC (permalink / raw)


mabes180@aol.com (Sarah Thomas) writes:

> i'm new to ada, could someone help me with the following:
> i need to read in an input file and parse each line word by word.
> I read in each line of text, but now i need to extract the words
> (separated by tabs) into an array, how do i do that...would appreciate
> some suggestions, thanks

See Ada.Strings.Fixed.

-- 
-- Stephe



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

* Re: how to parse words from a string
  2002-11-12 19:33 how to parse words from a string Sarah Thomas
  2002-11-12 20:15 ` Stephen Leake
@ 2002-11-12 20:16 ` Simon Wright
  2002-11-13 10:17 ` Caffeine Junky
  2 siblings, 0 replies; 15+ messages in thread
From: Simon Wright @ 2002-11-12 20:16 UTC (permalink / raw)


mabes180@aol.com (Sarah Thomas) writes:

> i'm new to ada, could someone help me with the following:
> i need to read in an input file and parse each line word by word.
> I read in each line of text, but now i need to extract the words
> (separated by tabs) into an array, how do i do that...would appreciate
> some suggestions, thanks

Finding the words .. obviously you're going to start with two indexes
into the line (start of word, end of word) set to the beginning of the
line and loop, incrementing the end of word variable, until you find
the end of the word.

Extracting the words .. do you know about slices? Line (First .. Last)
is a substring of the line.

Storing the words .. you'll need varying length "strings", check out
Ada.Strings.Unbounded.



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

* Re: how to parse words from a string
  2002-11-12 19:33 how to parse words from a string Sarah Thomas
  2002-11-12 20:15 ` Stephen Leake
  2002-11-12 20:16 ` Simon Wright
@ 2002-11-13 10:17 ` Caffeine Junky
  2002-11-14  2:10   ` Chad R. Meiners
  2 siblings, 1 reply; 15+ messages in thread
From: Caffeine Junky @ 2002-11-13 10:17 UTC (permalink / raw)


On Tue, 12 Nov 2002 14:33:39 -0500, Sarah Thomas wrote:

> i'm new to ada, could someone help me with the following: i need to read
> in an input file and parse each line word by word. I read in each line
> of text, but now i need to extract the words (separated by tabs) into an
> array, how do i do that...would appreciate some suggestions, thanks
 
One method I used was to declare some variables like this ...

Break : Character := Tab  --- Or space, or whatever --

subtype Word is String(1..Break);

Then create an array (or presort into a Tree) of Words.

 type Word_Stack is array(Positive range 1..Max) of Word;

Assuming of course your doing a buffer of some sort.

The rest is elementary. Check out the Ada.Strings packages for more in
depth info.

Caffeine Junky



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

* Re: how to parse words from a string
  2002-11-13 10:17 ` Caffeine Junky
@ 2002-11-14  2:10   ` Chad R. Meiners
  2002-11-14  2:40     ` Caffeine Junky
  0 siblings, 1 reply; 15+ messages in thread
From: Chad R. Meiners @ 2002-11-14  2:10 UTC (permalink / raw)



"Caffeine Junky" <nospam@hotmail.com> wrote in message
news:SEpA9.18107$L75.15886@rwcrnsc51.ops.asp.att.net...
> On Tue, 12 Nov 2002 14:33:39 -0500, Sarah Thomas wrote:
>
> One method I used was to declare some variables like this ...
>
> Break : Character := Tab  --- Or space, or whatever --
>
> subtype Word is String(1..Break);

This subtype declaration isn't valid Ada!





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

* Re: how to parse words from a string
  2002-11-14  2:10   ` Chad R. Meiners
@ 2002-11-14  2:40     ` Caffeine Junky
  2002-11-14  3:09       ` sk
  0 siblings, 1 reply; 15+ messages in thread
From: Caffeine Junky @ 2002-11-14  2:40 UTC (permalink / raw)


On Wed, 13 Nov 2002 21:10:23 -0500, Chad R. Meiners wrote:


> "Caffeine Junky" <nospam@hotmail.com> wrote in message
> news:SEpA9.18107$L75.15886@rwcrnsc51.ops.asp.att.net...
>> On Tue, 12 Nov 2002 14:33:39 -0500, Sarah Thomas wrote:
>>
>> One method I used was to declare some variables like this ...
>>
>> Break : Character := Tab  --- Or space, or whatever --
>>
>> subtype Word is String(1..Break);
> 
> This subtype declaration isn't valid Ada!
 
Strange. I didnt get any errors when compiling it. A character is an
element of a String. Maybe I'm missing something. I'll pull out the LRM
again.

Caffien Junky



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

* Re: how to parse words from a string
  2002-11-14  2:40     ` Caffeine Junky
@ 2002-11-14  3:09       ` sk
  2002-11-14  5:31         ` Dennis Lee Bieber
  0 siblings, 1 reply; 15+ messages in thread
From: sk @ 2002-11-14  3:09 UTC (permalink / raw)


Hi,

Caffeine Junky <nospam@hotmail.com>
> Strange. I didnt get any errors when compiling it. A character is an
> element of a String. Maybe I'm missing something. I'll pull out the LRM
> again.

procedure yyy is

    Break : Character := ASCII.HT; -- Horizontal Tab

    subtype Word is String (1 .. Break);

begin
    null;
end yyy;

>> gnatmake -v yyy
 
GNATMAKE 3.14p (20010503) Copyright 1995-2001 Free Software
Foundation, Inc.
  "yyy.ali" being checked ...
  -> "yyy.ali" missing.
gcc -c yyy.adb
yyy.adb:5:31: incompatible types in range
End of compilation
gnatmake: "yyy.adb" compilation error

A string is a numerically indexed array of characters in
simplest terms. So using a character as an index into a 
string should not compile under normal (Ada) circumstances.

-- 
-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
 k i e k c c m
-------------------------------------



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

* Re: how to parse words from a string
  2002-11-14  3:09       ` sk
@ 2002-11-14  5:31         ` Dennis Lee Bieber
  2002-11-14 13:40           ` Sarah Thomas
  0 siblings, 1 reply; 15+ messages in thread
From: Dennis Lee Bieber @ 2002-11-14  5:31 UTC (permalink / raw)


sk fed this fish to the penguins on Wednesday 13 November 2002 07:09 pm:

> 
> A string is a numerically indexed array of characters in
> simplest terms. So using a character as an index into a
> string should not compile under normal (Ada) circumstances.
> 

        And even if it did compile, I'd expect it (using a tab character) to 
basically be the same as    string(1..9) [presuming ASCII control 
code]... It definitely is not creating a dynamic length object for 
splitting text using a delimiter character.

-- 
 > ============================================================== <
 >   wlfraed@ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
 >      wulfraed@dm.net     |       Bestiaria Support Staff       <
 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <




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

* Re: how to parse words from a string
  2002-11-14  5:31         ` Dennis Lee Bieber
@ 2002-11-14 13:40           ` Sarah Thomas
  2002-11-14 14:56             ` David C. Hoos
  2002-11-14 18:45             ` Jeffrey Carter
  0 siblings, 2 replies; 15+ messages in thread
From: Sarah Thomas @ 2002-11-14 13:40 UTC (permalink / raw)


Interesting follow ups! thanks for the input and help ! 
I have succesfully extracted words from a string.
I read each line in from the file
and used find_token, followed by slice, followed by deleting the word
from the string..and then stored them in a fixed array for now..
this is just an outline of how i did it.....

loop
	Find_Token(Temp, Ada.Strings.Maps.To_Set(Ada.Characters.Latin_1.HT),
Ada.Strings.Outside, From, To);
	exit when To = 0;
		
	My_word := To_Unbounded_String(Slice(Temp, From, To));
	Put_line(Output_File,To_String(My_word));

	IF (Length(temp) /= To ) then
		Delete(Temp, 1, To + 1);
	else
		Delete(Temp, 1, To);
	end if;
			
	Store_data(Line_Number,Word_Number) := (My_Word);
			
end loop;



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

* Re: how to parse words from a string
  2002-11-14 13:40           ` Sarah Thomas
@ 2002-11-14 14:56             ` David C. Hoos
  2002-11-14 18:45             ` Jeffrey Carter
  1 sibling, 0 replies; 15+ messages in thread
From: David C. Hoos @ 2002-11-14 14:56 UTC (permalink / raw)



----- Original Message -----
From: "Sarah Thomas" <mabes180@aol.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, November 14, 2002 7:40 AM
Subject: Re: how to parse words from a string


> Interesting follow ups! thanks for the input and help !
> I have succesfully extracted words from a string.
> I read each line in from the file
> and used find_token, followed by slice, followed by deleting the word
> from the string..and then stored them in a fixed array for now..
> this is just an outline of how i did it.....
>
> loop
> Find_Token(Temp, Ada.Strings.Maps.To_Set(Ada.Characters.Latin_1.HT),
> Ada.Strings.Outside, From, To);
> exit when To = 0;
>
> My_word := To_Unbounded_String(Slice(Temp, From, To));
> Put_line(Output_File,To_String(My_word));
>
> IF (Length(temp) /= To ) then
> Delete(Temp, 1, To + 1);
> else
> Delete(Temp, 1, To);
> end if;
>
> Store_data(Line_Number,Word_Number) := (My_Word);
>
> end loop;
You could have saved the work of repeatedly deleting from the
Temp string by initially setting To := Length (Temp) - 1;.

Then if you make your call to Find_Token like this:

Find_Token(Slice (Temp, To + 1, Length (Temp)),
Ada.Strings.Maps.To_Set(Ada.Characters.Latin_1.HT),
Ada.Strings.Outside, From, To);

you just specify the unprocessed slice of Temp each time you look for a new
token.

I'm sure you realize you could have declared a String of word delimiters to
include
spaces, punctuation marks and other white space characters in the set of
possible
word delimiters, and used that String to initialize a Word_Delimiter_Set --
e.g.:

Word_Delimiter_Set : constant Ada.Strings.Maps.Character_Set :=
Ada.Strings.Maps.To_Set
(" ,.;:/!&()" & Ada.Characters.Latin_1.HT & Ada.Characters.Latin_1.FF);






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

* Re: how to parse words from a string
  2002-11-14 13:40           ` Sarah Thomas
  2002-11-14 14:56             ` David C. Hoos
@ 2002-11-14 18:45             ` Jeffrey Carter
  2002-11-17  8:40               ` Mário Amado Alves
  1 sibling, 1 reply; 15+ messages in thread
From: Jeffrey Carter @ 2002-11-14 18:45 UTC (permalink / raw)


Sarah Thomas wrote:
> Interesting follow ups! thanks for the input and help ! 
> I have succesfully extracted words from a string.
> I read each line in from the file
> and used find_token, followed by slice, followed by deleting the word
> from the string..and then stored them in a fixed array for now..

Another approach is to parse the words as you read the file; for an 
example, see PragmARC.Word_Input from the PragmAda Reusable Components

http://home.earthlink.net/~jrcarter010/pragmarc.htm

-- 
Jeff Carter
"I spun around, and there I was, face to face with a
six-year-old kid. Well, I just threw my guns down and
walked away. Little bastard shot me in the ass."
Blazing Saddles




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

* Re: how to parse words from a string
  2002-11-14 18:45             ` Jeffrey Carter
@ 2002-11-17  8:40               ` Mário Amado Alves
  2002-11-17 21:14                 ` Jeffrey Carter
  0 siblings, 1 reply; 15+ messages in thread
From: Mário Amado Alves @ 2002-11-17  8:40 UTC (permalink / raw)


> Another approach is to parse the words as you read the file; for an 
> example, see PragmARC.Word_Input from the PragmAda Reusable Components
> 
> http://home.earthlink.net/~jrcarter010/pragmarc.htm

Or simply write:

  C : Character
  Word : Unbounded_String;
begin
  loop
    Get_Immediate (C);
    if Is_Graphic (C) and C /= ' ' then
      Append (Word, C);
    else
      Process (Word);
      Word := Null_Unbounded_String;
    end if;
  end loop;
exception
  when End_Error =>
    if Word /= Null_Unbounded_String then Process (Word); end if;

as was recently posted on thread "parse word from a string" ;-)

--MAA



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

* Re: how to parse words from a string
  2002-11-17  8:40               ` Mário Amado Alves
@ 2002-11-17 21:14                 ` Jeffrey Carter
  2002-11-18 11:00                   ` Mário Amado Alves
  0 siblings, 1 reply; 15+ messages in thread
From: Jeffrey Carter @ 2002-11-17 21:14 UTC (permalink / raw)


M�rio Amado Alves wrote:
>>Another approach is to parse the words as you read the file; for an 
>>example, see PragmARC.Word_Input from the PragmAda Reusable Components
>>
>>http://home.earthlink.net/~jrcarter010/pragmarc.htm
> 
> 
> Or simply write:
> 
>   C : Character
>   Word : Unbounded_String;
> begin
>   loop
>     Get_Immediate (C);
>     if Is_Graphic (C) and C /= ' ' then
>       Append (Word, C);
>     else
>       Process (Word);
>       Word := Null_Unbounded_String;
>     end if;
>   end loop;
> exception
>   when End_Error =>
>     if Word /= Null_Unbounded_String then Process (Word); end if;
> 
> as was recently posted on thread "parse word from a string" ;-)

which doesn't read from an arbitrary file nor handle words separated by 
horizontal tabs.

-- 
Jeff Carter
"English bed-wetting types."
Monty Python & the Holy Grail




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

* Re: how to parse words from a string
  2002-11-17 21:14                 ` Jeffrey Carter
@ 2002-11-18 11:00                   ` Mário Amado Alves
  2002-11-18 19:23                     ` Jeffrey Carter
  0 siblings, 1 reply; 15+ messages in thread
From: Mário Amado Alves @ 2002-11-18 11:00 UTC (permalink / raw)


> >   C : Character
> >   Word : Unbounded_String;
> > begin
> >   loop
> >     Get_Immediate (C);
> >     if Is_Graphic (C) and C /= ' ' then
> >       Append (Word, C);
> >     else
> >       Process (Word);
> >       Word := Null_Unbounded_String;
> >     end if;
> >   end loop;
> > exception
> >   when End_Error =>
> >     if Word /= Null_Unbounded_String then Process (Word); end if;
 
> ... doesn't read from an arbitrary file

Trivial adaptation, left as an exercise. Hints: see A.10.1 (15) and (41).

> nor handle words separated by horizontal tabs.

Of course it does. See A.3.2 (23).

--MAA



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

* Re: how to parse words from a string
  2002-11-18 11:00                   ` Mário Amado Alves
@ 2002-11-18 19:23                     ` Jeffrey Carter
  0 siblings, 0 replies; 15+ messages in thread
From: Jeffrey Carter @ 2002-11-18 19:23 UTC (permalink / raw)


M�rio Amado Alves wrote:
>>nor handle words separated by horizontal tabs.
> 
> 
> Of course it does. See A.3.2 (23).

Sorry. I omitted "only" between "separated" and "by". Certainly what you 
posted works for a certain set of requirements that could be called 
"parsing words from a file", but not for the OP's stated requirements.

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail




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

end of thread, other threads:[~2002-11-18 19:23 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-12 19:33 how to parse words from a string Sarah Thomas
2002-11-12 20:15 ` Stephen Leake
2002-11-12 20:16 ` Simon Wright
2002-11-13 10:17 ` Caffeine Junky
2002-11-14  2:10   ` Chad R. Meiners
2002-11-14  2:40     ` Caffeine Junky
2002-11-14  3:09       ` sk
2002-11-14  5:31         ` Dennis Lee Bieber
2002-11-14 13:40           ` Sarah Thomas
2002-11-14 14:56             ` David C. Hoos
2002-11-14 18:45             ` Jeffrey Carter
2002-11-17  8:40               ` Mário Amado Alves
2002-11-17 21:14                 ` Jeffrey Carter
2002-11-18 11:00                   ` Mário Amado Alves
2002-11-18 19:23                     ` Jeffrey Carter

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