Ada String Issue: String within Strings 
Author Message
 Ada String Issue: String within Strings

Just a little hello and little comment that I've enjoyed reading
through this newsgroup.

My problem is this:  I am Get_Line-ing from a file... what I want to
do is check that line of text for a particular phrase given (not just
a word).  I've tried using Index() and I get an Error that says...

extract.adb:73:66: array type required in indexed component

The code reads:
   Get_Line (f_From, LineItem, Index);
   while (not End_Of_File(f_From)) loop
      Found := 0;
      Found := Index (LineItem(1..Index), Catch);
      if Found > 0 then
        Put_Line(f_To, LineItem(1..Index));
      end if;
   end loop;    

where
LineItem, Catch : String(1..1024);
Found : Natural;

Is what I am doing the right idea or is there a different built in
function that will do what C does in comparing the text and returning
the location of where it starts?

Thanks in advance
Andrew



Tue, 18 Nov 2003 21:31:32 GMT  
 Ada String Issue: String within Strings

Quote:

> Just a little hello and little comment that I've enjoyed reading
> through this newsgroup.

> My problem is this:  I am Get_Line-ing from a file... what I want to
> do is check that line of text for a particular phrase given (not just
> a word).  I've tried using Index() and I get an Error that says...

> extract.adb:73:66: array type required in indexed component

> The code reads:
>    Get_Line (f_From, LineItem, Index);
>    while (not End_Of_File(f_From)) loop
>       Found := 0;
>       Found := Index (LineItem(1..Index), Catch);
>       if Found > 0 then
>         Put_Line(f_To, LineItem(1..Index));
>       end if;
>    end loop;

> where
> LineItem, Catch : String(1..1024);
> Found : Natural;

> Is what I am doing the right idea or is there a different built in
> function that will do what C does in comparing the text and returning
> the location of where it starts?

> Thanks in advance
> Andrew

I assume you are using GNAT.

Try changing the name of your "Index" variable to something else. The
following code...

with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
procedure A is
   LineItem : String(1..10) := "  ABC     ";
   Catch : String(1..10) := "ABC       ";
   Jndex : Natural := 3;
   Found : Natural;

begin
   Found := Index (LineItem(1..Jndex), Catch);
end A;

does not have the same problem. Note that I renamed the local "Index" to
"Jndex" for this to work. An alternative is...

with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
procedure A is
   LineItem : String(1..10) := "  ABC     ";
   Catch : String(1..10) := "ABC       ";
   Index : Natural := 3;
   Found : Natural;

begin
   Found := Ada.Strings.Fixed.Index (LineItem(1..Index), Catch);
end A;

which is a little more explicit on "which Index" you are referring to.
  --Mark

[and yes, I noticed I probably have the LineItem and Catch values
assigned wrong....]



Tue, 18 Nov 2003 22:04:11 GMT  
 Ada String Issue: String within Strings

Quote:

> Just a little hello and little comment that I've enjoyed reading
> through this newsgroup.

> My problem is this:  I am Get_Line-ing from a file... what I want to
> do is check that line of text for a particular phrase given (not just
> a word).  I've tried using Index() and I get an Error that says...

> extract.adb:73:66: array type required in indexed component

(Code Deleted)

> Is what I am doing the right idea or is there a different built in
> function that will do what C does in comparing the text and returning
> the location of where it starts?

Look at the generic package Ada.Strings.Bounded. It includes
several search functions for substrings within strings.

These functions are, at an abstract level, similar to the C
function strstr().

Jim Rogers
Colorado Springs, Colorado USA



Tue, 18 Nov 2003 22:04:19 GMT  
 Ada String Issue: String within Strings

Quote:
>extract.adb:73:66: array type required in indexed component

>The code reads:
>   Get_Line (f_From, LineItem, Index);
>   while (not End_Of_File(f_From)) loop
>      Found := 0;
>      Found := Index (LineItem(1..Index), Catch);
>      if Found > 0 then
>    Put_Line(f_To, LineItem(1..Index));
>      end if;
>   end loop;        

It appears that the compiler is getting confused between your variable "Index",
and the subprogram "Index". In fact, your local declaration of the variable is
probably (silently) hiding the function.

This is one of the reasons why I *strongly* suggest that any beginner aviod the
"use" clause for packages. "use type" is generally OK, but "use" for packages is
very contraversial for exactly this reason: it can make things rather confusing.
Personally, I don't like to use it *ever*, but there are those who strongly
disagree with this policy. The safest thing for a beginner to do is to just
avoid the whole issue by using full notation for everything until you get enough
experience to make an intelligent decision on the issue yourself.

Try it yourself, and see what happens. The above turns into:

Ada.Text_IO.Get_Line (f_From, LineItem, Index);
while (not Ada.Text_IO.End_Of_File(f_From)) loop
Found := 0;
Found := Ada.Strings.Fixed.Index (LineItem(1..Index), Catch);
if Found > 0 then
Ada.Text_IO.Put_Line(f_To, LineItem(1..Index));
end if;
end loop;      

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



Tue, 18 Nov 2003 22:43:35 GMT  
 Ada String Issue: String within Strings

Quote:
>Ada.Text_IO.Get_Line (f_From, LineItem, Index);
>while (not Ada.Text_IO.End_Of_File(f_From)) loop
>Found := 0;
>Found := Ada.Strings.Fixed.Index (LineItem(1..Index), Catch);
>if Found > 0 then
>Ada.Text_IO.Put_Line(f_To, LineItem(1..Index));
>end if;
>end loop;  

Yikes! Please ignore my lack of formatting. That was my posting software
speaking, not me.

To answer your other question: yes, you do seem to be going about this the right
way. The only other "peer review" comment I would make is:

The two assignments into "Found" (and thus the "Found" variable itself) are
unnessecary. You could remove them both and change the if statement to:

if Ada.Strings.Fixed.Index (LineItem(1..Index), Catch) > 0 then

Some might leave in the second assignment, but the first is completely unneeded.

Also, it appears that you are not reading from the file within the loop. That
means that the file pointer will not advance, and thus you will never reach the
end of the file (unless the very first read gets you there), and thus the loop
will iterate endlessly.

Also, if the first read *does* read the only line in the file, the contents of
what it read will never be checked.

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



Tue, 18 Nov 2003 23:07:01 GMT  
 Ada String Issue: String within Strings
Quote:
>  LineItem : String(1..10) := "  ABC     ";
>  Catch : String(1..10) := "ABC       ";

  Since Catch is the same length as LineItem it can either match
starting at the first character (which this doesn't) or not at all.
You probably want
   Catch : String(1..3) := "ABC";

Quote:
>  Jndex : Natural := 3;

  "Last" is a commonly used name.


Wed, 19 Nov 2003 01:43:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. string variable within a string

2. string variables within strings

3. String insert within a string (src code included)

4. string = string(i:j) // string(k:n)

5. BUG: Scientific Number to String/String to Number

6. Picture to String/String to Picture

7. string variable and string field

8. How to convert binary string to char string ?

9. CW strings (was CW1501 strings)

10. Array to String and String to Array

11. Proposal v2 /was: Standardising string primitives (was: Re: String)

 

 
Powered by phpBB® Forum Software