Solved

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

Posted on 1997-05-22
8
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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