Solved

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

Posted on 1997-05-22
8
228 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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