Board index » delphi » BottleNeck

BottleNeck

I am Currently in the process of doing my Assignment.

IT involves reading a Text file, and storing it into parallel
one dimensional arrays. I have to read the text file char by char, and
then add the completed string into the array. I also have to compare the
value in the array cells and determine how may times the string is
repeated, and update the corresponding numArray to reflect the
repetition occurrence. The text file is Peter Piper Picked....
I am Bottlenecked at the point where i have to read the text file char,
by char, and then form a word....I have inluced my source code (
partial).

Program Tongue_Twister;
{
                                Assignment 2
                              By Riyaz Mohamed
                                 Comp 1450
                              Student A00185875

   This Program, will prompt the user to indicate a text file that the
   program will process. The Program will sort the Text file by
individual
   words , and record their occurence. This information will then be
   displayed back to the user.

Quote
}

{
#######################################################################}

Uses Crt;

const min=1 ;
      max =50;

type  WordArr = Array[min..max] of string;
      NumArr = Array[min..max] of integer;

Var   Word  : WordArr;
      Count : NumArr;
      Letter : Char;
      Num : integer;
      FileName : String;
      MyFile : text;

{
########################################################################

Quote
}

Procedure PauseScreen;                  { Pauses Screen and Prompts user
for}
                                        { Their
input'                      }
Begin
        Writeln;
        GoToXY(20,10);writeln(' Press Enter to Continue');
        Readln;
        Clrscr
End;

{ ######################################################################

Quote
}

Procedure InitializeArray;               { Sets Values of the Array
fields to nil (no value) }

Begin
        For Num := min to max Do;
            Begin
                 Word[num] := '';
                 Count[num]:= 0
            End
End;

{ ######################################################################

Quote
}

Procedure AskFile;
                                       { Prompts User to Enter a
Filename }
Begin
        GotoXY(20,10);Writeln('Please Enter Filename ');
        GoToXY(20,12);Readln( FileName );
        Assign (MyFile, Filename );
        Reset (Myfile)

End;

{#######################################################################

Quote
}

Procedure Validate(tmpwrd:string);

Var Index : Integer;

    Begin
         Index := 1;
         While Index < ( 50 ) DO
               Begin
                    If Tmpwrd = Word[index] then
                       Begin
                            Count[Index] := Count[Index] +1;
                            Index := 99
                       End
                    Else
                        If Word[Index] = '' Then
                           Begin
                                Word[Index] := Tmpwrd;
                                Count[INdex] := 1;
                                Index :=99
                           End
                        Else
                            Index := Index + 1
               End
    End;

{
#######################################################################}

Procedure Readfile;

Var  Num : integer;
     TmpWrd  : String[30];
     Letter : Char;

Begin
     Num := 0;
     While NOT EOF (MyFile) Do
           Begin
                 Num := Num + 1;
                 Read (MyFile, Tmpwrd[Num]);
                 TmpWrd := upcase(Tmpwrd[Num]);
                        If TmpWrd[Num] = '' then
                           Validate

           End

End;

Begin
     Pausescreen;
     InitializeArray;
     AskFile;
     Readfile;
     Validate
End.

Appreciate the help.

Regards R. Mohamed

 

Re:BottleNeck


Quote
"Riyaz Mohamed" <rmoha...@home.com> wrote in message

news:3B612AB7.568622F6@home.com...

Quote
> I am Bottlenecked at the point where i have to read the text file char,
> by char, and then form a word....I have inluced my source code (
> partial).

> const min=1 ;
>       max =50;

Note: in procedure Validate() you should use these rather than 1 and 50
for your limits on Index (and change '99' to something that's always
going to be higher than max, such as max + 1). I think you also want
while (index <= 50) rather than while (index < 50) there. It'd also be
a good idea to return a status code to indicate if you've run out of
room in your array.

Anyway, to the main point:

Quote
> Procedure Readfile;
> Var  Num : integer;
>      TmpWrd  : String[30];
>      Letter : Char;
> Begin
>      Num := 0;
>      While NOT EOF (MyFile) Do
>            Begin
>                  Num := Num + 1;
>                  Read (MyFile, Tmpwrd[Num]);
>                  TmpWrd := upcase(Tmpwrd[Num]);
>                         If TmpWrd[Num] = '' then
>                            Validate
>            End
> End;

Since you're working with a file of type text, you'll need to handle
eoln() as well as eof().

It's probably not a good idea to limit words to 30 characters,
especially without any error reporting.

The line TmpWrd := UpCase(TmpWrd[Num]); is in error - it sets the
*whole string* to the uppercase version of the current letter.
Probably you meant TmpWrd[Num] := UpCase(TmpWrd[Num]);

You need to call Validate with TmpWrd as a parameter - this is
another reason not to make TmpWrd a fixed-length string here.

The main problem seems to be the end-of-word condition, though; you're
checking to see if the next character read is '', which will happen
at most at every end of line and end of file (unchecked).
Define a set of characters to be considered "whitespace": you'll
want space and tab at least in here (CR/LF are taken care of already
by the text file mechanism and will give you eoln() instead). In a
real application you'd probably want to consider punctuation, numbers,
etc. separately but we'll just pretend they don't exist for now. ;-)
So each line is made up of words separated by spaces and tabs (char #9).

Finally, it's easier and more readable if you call a procedure to
read each line.

Here's an example for you to try. Note that I haven't tested this code.

procedure Readline;
const
  whitespace = [' ', #9];  {space and tab}
var
  tmpwrd: string;
  letter: char;
  done, inword: Boolean;
begin
  tmpwrd := '';
  repeat  {skip whitespace}
    read(myfile, letter);
    inword := not (letter in whitespace)
  until inword or eoln(myfile);
  while not eoln(myfile) do
    begin
      repeat
        tmpwrd := tmpwrd + letter;
        read(myfile, letter);
        inword := not (letter in whitespace)
      until not inword or eoln(myfile);
      if inword then
        tmpwrd := tmpwrd + letter;
      Validate(tmpwrd);
      tmpwrd := '';
      while not (inword or eoln(myfile)) do
        begin
          read(myfile, letter);
          inword := not (letter in whitespace)
        end
    end
end;

procedure Readfile;
var
  done: Boolean;
begin
  done := eof(myfile);
  while not done do
    begin
      while eoln(myfile) and not eof(myfile) do
        readln(myfile); {skip any empty lines}
      if not eof(myfile) then
        ReadLine;  {read a line}
      done := eof(myfile);
    end;
end;

--
The Scarlet Manuka

Re:BottleNeck


Scarlet,

Many thx ,

Your Code helped shed a Ton of light on my problem.
I had not considered using a boolean to flag the ''
condition, or the EOF condtion.

Your Idea of Calling the Readline Proc, in Readfile.
Helped me track the Logic easier.

Once Again,

Many Thanks.

Regards

Riyaz Mohamed

Grateful Pascal Newb. :)

Re:BottleNeck


Quote
"Riyaz Mohamed" <rmoha...@home.com> wrote in message

news:3B67A0D3.761C6E28@home.com...

Quote
> Scarlet,

> Many thx ,

No problem!

Quote
> Your Code helped shed a Ton of light on my problem.
> I had not considered using a boolean to flag the ''
> condition, or the EOF condtion.

I like booleans. ;-) They're handy for keeping
track of complicated status conditions.

Quote
> Your Idea of Calling the Readline Proc, in Readfile.
> Helped me track the Logic easier.

Well, that's the idea! In general if a procedure
is long and complex it's a good idea to try to think
of it in terms of smaller parts.

Keep going, it's definitely worth the effort!
And remember, there's no substitute for lots
and lots of practice; the more programs you
code up, the better you'll get at doing it.
Especially if you have to debug them all a
few months later!

--
The Scarlet Manuka

Other Threads