comp.lang.ada
 help / color / mirror / Atom feed
* Ada String Issue: String within Strings
@ 2001-06-01 13:31 Xcalibre
  2001-06-01 14:04 ` Mark Johnson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Xcalibre @ 2001-06-01 13:31 UTC (permalink / raw)


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



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

* Re: Ada String Issue: String within Strings
  2001-06-01 13:31 Ada String Issue: String within Strings Xcalibre
@ 2001-06-01 14:04 ` Mark Johnson
  2001-06-01 17:43   ` tmoran
  2001-06-01 14:04 ` James Rogers
  2001-06-01 14:43 ` Ted Dennison
  2 siblings, 1 reply; 6+ messages in thread
From: Mark Johnson @ 2001-06-01 14:04 UTC (permalink / raw)


Xcalibre wrote:

> 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....]





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

* Re: Ada String Issue: String within Strings
  2001-06-01 13:31 Ada String Issue: String within Strings Xcalibre
  2001-06-01 14:04 ` Mark Johnson
@ 2001-06-01 14:04 ` James Rogers
  2001-06-01 14:43 ` Ted Dennison
  2 siblings, 0 replies; 6+ messages in thread
From: James Rogers @ 2001-06-01 14:04 UTC (permalink / raw)


Xcalibre wrote:
> 
> 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



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

* Re: Ada String Issue: String within Strings
  2001-06-01 13:31 Ada String Issue: String within Strings Xcalibre
  2001-06-01 14:04 ` Mark Johnson
  2001-06-01 14:04 ` James Rogers
@ 2001-06-01 14:43 ` Ted Dennison
  2001-06-01 15:07   ` Ted Dennison
  2 siblings, 1 reply; 6+ messages in thread
From: Ted Dennison @ 2001-06-01 14:43 UTC (permalink / raw)


In article <4713a80b.0106010531.7f6f4da8@posting.google.com>, Xcalibre 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;	

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
          home email - mailto:dennison@telepath.com



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

* Re: Ada String Issue: String within Strings
  2001-06-01 14:43 ` Ted Dennison
@ 2001-06-01 15:07   ` Ted Dennison
  0 siblings, 0 replies; 6+ messages in thread
From: Ted Dennison @ 2001-06-01 15:07 UTC (permalink / raw)


In article <rSNR6.592$v4.18766@www.newsranger.com>, Ted Dennison says...
>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
          home email - mailto:dennison@telepath.com



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

* Re: Ada String Issue: String within Strings
  2001-06-01 14:04 ` Mark Johnson
@ 2001-06-01 17:43   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2001-06-01 17:43 UTC (permalink / raw)


>  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";

>  Jndex : Natural := 3;
  "Last" is a commonly used name.



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

end of thread, other threads:[~2001-06-01 17:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-01 13:31 Ada String Issue: String within Strings Xcalibre
2001-06-01 14:04 ` Mark Johnson
2001-06-01 17:43   ` tmoran
2001-06-01 14:04 ` James Rogers
2001-06-01 14:43 ` Ted Dennison
2001-06-01 15:07   ` Ted Dennison

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