Solved

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

Posted on 1997-05-22
8
219 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now