Fast Way To Amend Data Stringlist or InI

eNarc
eNarc used Ask the Experts™
on
Hi, I'm looking for a fast way to Amend/Edit strings.

I'm currently wanting a wast way, InI are slow though Stringlist is only able to hold 1 line.. and how do I edit that line without searching for that line?

and if I've got about 50,000 lines, then its going to be really slow, though having to open the InI 50,000x just to edit, isn't fast either..

what should I do?


the line is this



C:\folder\*29848484


I'm needing to change the numbers amend them when needed.

like I need to do this.


insert 34242323 into C:\folder\

so from C:\folder\*29848484 becomes C:\folder\*34242323
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Geert GOracle dba
Top Expert 2009
Commented:
use a tool like grep

you can use regular expressions and it sits in the delphi bin folder and is blazing fast
Geert GOracle dba
Top Expert 2009

Commented:
there is also an extensive help in the delphi help itself

Author

Commented:
grep? how would I use that for the above example?

Commented:
TStringList is not slow,  maybe your code does not totally find the right way.

//Form Create
Slist := TStringList.Creat;
...
....
//Access it
//You can block duplicates to lower your list
Slist.Duplicate := dupIgnore;
if Slist.indexOf(YourList) = -1 then Slist.Add(YourList);
..
...
/Amend//Edit Strings
For i := 0 to Slist.Count-1 do
begin
   if Slist.Find('YourFindString', i) = true
   then
   begin
   Slist.Add('YourNewString');
   Slist.Exchange(i, SList.IndexOf('YourNewString'));
   Slist.Delete(SList.IndexOf('YourNewString'));
   end;
end;

Author

Commented:
I've done it this way, I've added to tstringlists to the form.

one to hold the folder name, the other for the size as I've done this because the output needs to be

C:\Folder Name\*29749872

and if I wanted to use one SL then I'd have to parse each item, and that could take a while so I've split it.

function EditFolderSize(Path,Value:string):string;
var
  i,o:integer;
begin
  o:=0;
  for I := 0 to SL1.Count - 1 do begin
    if SL1[i] = Path then begin
      SL2[i]:=Value;
      o:=1;
    end;
  end;
  if o = 0 then begin
    SL1.Add(Path);
    SL2.Add(Value);
  end;
end;

this way seems to edit the list perfectly while keeping the list correct line to line.

if there a simplier way of doing this? at the same time shorting the function or making it neater?
Commented:
Youre using a function, and where is your return? or result? value.

Use procedure instead of function if you dont want to return a value.

Try the code below;


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)

    Button1: TButton;
    Button2: TButton;

    Edit1: TEdit;
    Edit2: TEdit;

    ListBox1: TListBox;


    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  SL1, SL2:TStringList;

implementation

{$R *.dfm}

function EditFolderSize(Path,Value:string):string;
begin
if SL1.indexOf(Path) = -1 then
begin
SL1.Add(Path);
SL2.Add(Value);
result:='1';
end
else
result:='0';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if EditFolderSize(Edit1.Text, Edit2.Text)='1'
then
showmessage('added successfuly')
else
showmessage('failed to add')
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SL1 := TStringList.Create;
SL2 := TStringList.Create;
SL1.Duplicates  := dupIgnore;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Items.Add(SL1.DelimitedText);
ListBox1.Items.Add(SL2.DelimitedText);
end;

end.

Open in new window

Commented:
Shorter,  Faster and Neater procedure;
Procedure EditFolderSize(Path,Value:string);
begin
if SL1.indexOf(Path) = -1 then
begin
SL1.Add(Path);
SL2.Add(Value);
end;
end;



//Include it at Form Create
procedure TForm1.FormCreate(Sender: TObject);
begin
SL1 := TStringList.Create;
SL2 := TStringList.Create;
SL1.Duplicates  := dupIgnore;
end;

Open in new window

Top Expert 2014

Commented:
@systan

The delimitedtext method might not be reliable with extended path names, since deleimitedtext always includes spaces in addition to the supplied delimiter.

Commented:
aikimark;
I have no delimitedtext method here, it migth be in my other post.
Ephraim WangoyaSoftware Engineer

Commented:
Why not use the name value pairs, the TStringList is prety fast

List.Values[Folder] := IntToStr(FolderSize)
 
if you want to search for the folder, you can use
List.IndexOfName[Folder]

This way you don't have to keep track of two lists
Top Expert 2014

Commented:
>>I have no delimitedtext method here,
From http:#33155935
ListBox1.Items.Add(SL1.DelimitedText);
ListBox1.Items.Add(SL2.DelimitedText);

Open in new window

Commented:
Oh, theres nothing to do with the asker, I just added that to show that
my code in
Procedure EditFolderSize(Path,Value:string);
is working fine, shorter, faster, neater.

Because the asker wants to neat and shorter and faster code.

It's just temporary intended for viewing if it exist.

Author

Commented:
>>ewangoya
Why not use the name value pairs, the TStringList is prety fast

List.Values[Folder] := IntToStr(FolderSize)
--------------------

only thing i about using SL.Value is that what if the folder has '=' in? as it will bring back null. cos the sl is folder=size within the sl.

Author

Commented:
how u get that link like that? http:#33155935
Top Expert 2014

Commented:
I typed the link, as you see it, in the comment box.  Since it refers to a comment in the current thread, it doesn't need the site name.  One day, we might be able to have the link copied to the clipboard by clicking on the comment number. (it is a requested feature, albeit a low priority one)

Author

Commented:
for some reason cant seem to accept the solutions? who do I contact regarding this?

I'm wanting to accept http:#33155222 while giving everyone equal share in points. though its saying all points must be 25 and each was 45 to all comments provided in this page.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial