Solved

Read txt file to Tstrlist extract dups read back to new txt file

Posted on 1997-05-22
8
230 Views
Last Modified: 2010-04-04
I have a list of phone #'s in a txt file.  I want to read them out of the file into a Tstrlist (or what ever is best), remove any duplicates and then put them Back into a new txt file without any duplicates.  There are about 10,000-20,000 phone #'s in the txt file.  P.S> I also want to eliminate any blank lines and also want to append data to the new file later
0
Comment
Question by:hijinx
  • 4
  • 4
8 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 150 total points
ID: 1336522
There are faster ways to do this, but it works.
(20,000 16 char strings took about 6s on my machine)

procedure TMainForm.Button8Click(Sender: TObject);
var
  Index   : Integer;
  StrList : TStringList;
  TempStr : string;
begin
  Screen.Cursor := crHourGlass;
  Strlist := TStringList.Create;
  try
    StrList.LoadFromFile('C:\SLASK.TXT');
    StrList.Sort;
    TempStr := '';
    Index := 0;
    repeat
      if (StrList[Index]=TempStr)
      then begin
        StrList.Delete(Index);
      end else begin
        TempStr := StrList[Index];
        Inc(Index);
      end;
    until (Index=StrList.Count);
    StrList.SaveToFile('C:\SLASK2.TXT');
  finally
    StrList.Free;
    Screen.Cursor := crDefault;
  end;
end;

/// John
0
 

Author Comment

by:hijinx
ID: 1336523
Edited text of question
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1336524
In what way do you mean append?


procedure TMainForm.Button8Click(Sender: TObject);
var
  Index   : Integer;
  StrList : TStringList;
  TempStr : string;
begin
  Screen.Cursor := crHourGlass;
  Strlist := TStringList.Create;
  try
    StrList.LoadFromFile('C:\SLASK.TXT');
    StrList.Sort;
    TempStr := '';
    Index := 0;
    repeat
      if (StrList[Index]=TempStr)
      or (StrList[Index]='') // <<< Here!
      then begin
        StrList.Delete(Index);
      end else begin
        TempStr := StrList[Index];
        Inc(Index);
      end;
    until (Index=StrList.Count);
    StrList.SaveToFile('C:\SLASK2.TXT');
  finally
    StrList.Free;
    Screen.Cursor := crDefault;
  end;
end;

/// John
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hijinx
ID: 1336525
What I really mean is that I want to create a file, read character data into it from another file.  Sort that file for duplicates then add that data to a third file that already has sorted unduplicated data in it.  I've done this by creating two sorted unduplicate files using the little program you sent and then reading the data from both files into a third file.  This is not a very elegant or speedy way to do this.  Especially if each file has a lot of data in it,   say 100,000 records each of 75-100 characters.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1336526
If you want higher speeds, you have to increase the points.
Maybe you can use a hashtable for the duplicate checking?
Are the records of uniform length?

/// John
0
 

Author Comment

by:hijinx
ID: 1336527
John,

The records vary between 10 to 75 characters, none shorter then 10 and none longer then 75.  However, you know, I think the way I'm doing it will be OK.  Thanks a lot for your help.  

I will try a hash table and see if it works any better though


regards
0
 

Author Comment

by:hijinx
ID: 1336528
John,

One further  annd I hope final question.  I want to use a construct to find and extract a string after a word.  So I guess I need to  use IndexOf is this the correct Construct:

"If IndexOf(TstrList[Index]: 'And:') > -1
      Then Begin"


0
 
LVL 4

Expert Comment

by:erajoj
ID: 1336529
If the string you're looking for exactly (case insensitively) matches 'And:' then it will work, otherwise:
you'll have to use brute-force + "Pos" or divide&conquer + "Pos"
or some other method.
If you need an example or further help, then give me my points or reject my answer so that someboby else can help you!
Tip: do not use "TstrList" as an identifier, simply use "StrList"
or something else. "T" is defacto used as a "type/class" prefix.
This is to make it easier later on.

/// John
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question