Solved

Fastest Way to Extract Record from a Long Semicolon Delimited WideString?

Posted on 2004-04-07
15
377 Views
Last Modified: 2010-04-05
I am writing a function to get data from server and process accordingly. The data receive from server is in a semi colon delimited widestring format.

For example,

var
  Input: WideString;
  i, TotalRec: Integer;

begin
  TotalRec := GetData(Input);
  // Input will have values like 'leon,24,89,100;jimmy,30,77,56;'
  for i := 0 to TotalRec do
  // TotalRec could range from 5,000 - 20,000
  // I need to extract each record to process
  // What's the fastest way to do this?
end;

0
Comment
Question by:coole
  • 5
  • 3
  • 3
  • +4
15 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10773125
unit Unit_Q_20946183;

interface

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

type
  TForm1 = class(TForm)
      SpeedButton1: TSpeedButton;
      ListBox1: TListBox;
      procedure SpeedButton1Click(Sender: TObject);
    private   { Private declarations }
    public    { Public declarations }
      procedure ExtractFromString(Input: WideString);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ExtractFromString(Input: WideString);
var
//  i, TotalRec: Integer;
  P:      Integer;
  S:      string;
  procedure Extract(S: string);
  begin
    ListBox1.Items.Add(S);
    Delete(Input, 1, P);
    P := Pos(';',Input);
  end;
begin
  {
  TotalRec := GetData(Input);
  // Input will have values like 'leon,24,89,100;jimmy,30,77,56;'
  for i := 0 to TotalRec do
  // TotalRec could range from 5,000 - 20,000
  // I need to extract each record to process
  // What's the fastest way to do this?
  }
  P := Pos(';',Input);
  while (P>0) do
  begin
    S := Copy(Input, 1, P-1);
    Extract(S);
  end;
  if (Input<>'') then
    Extract(Input);
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  ExtractFromString('Leon,24,89,100;Jimmy,30,77,56');
end;

end.

emil
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10773130
object Form1: TForm1
  Left = 199
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object SpeedButton1: TSpeedButton
    Left = 36
    Top = 22
    Width = 103
    Height = 22
    Caption = 'Extract'
    OnClick = SpeedButton1Click
  end
  object ListBox1: TListBox
    Left = 150
    Top = 2
    Width = 531
    Height = 441
    ItemHeight = 13
    TabOrder = 0
  end
end
0
 
LVL 2

Expert Comment

by:xxflip
ID: 10773154
You can do as follows:

procedure ParseRecData;
var
 ml:TStrings;
 Input:WideString;
begin
  // Input:=  ----> Method to get Input from server
  ml:=TStringList.Create;
  try
    if Input[1]=';' then Delete(Input,1,1);
    while Pos(';',Input) <> 0 do begin
      ml.Add(copy(Input,1,Pos(';',Input) - 1));
      Delete(Input,1,Pos(';',Input));
    end;
    if length(Input) <> 0 then ml.Add(Input);
    Memo1.Lines.Assign(ml);
  finally
    ml.Free;
  end;
end;
0
 
LVL 2

Expert Comment

by:xxflip
ID: 10773166
boy, one has to be quick if he wishes to be the first :-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10773212
Next code is better:

procedure TForm1.ExtractFromString(Input: WideString);
var
  P:      Integer;
  S:      string;
  procedure Extract(S: string);
  begin
    ListBox1.Items.Add(S);
    Delete(Input, 1, P);
    P := Pos(';',Input);
  end;
begin
  P := Pos(';',Input);
  while (P>0) do
  begin
    S := Copy(Input, 1, P-1);
    Extract(S);
  end;
  if (Input<>'') then
    ListBox1.Items.Add(S);
end;

emil
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10773218
 if (Input<>'') then
    ListBox1.Items.Add(Input);
0
 
LVL 3

Expert Comment

by:SuperUt
ID: 10773321
Forget any solution using Delete on the very large 'Input' string or Input:= copy( Input, xxx).
This will take a huge amount of time since the string is copied in memory over and over.

Point is to keep the Input string intact so that it isn't moved around in memory.
You could scan the string for the position of the delimiter and take the data out of it.
You can do this using AnsiStrPos(Str, SubStr: PChar) where you move the begin pointer in the string until you reach the end of the string.

Another solution could be to replace the delimiter by a CRLF and paste that string in the text property of a stringlist.
Then you can directly browse the stringlist.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10773361
what about this?

procedure TForm1.Button1Click(Sender: TObject);
procedure GetRecords(var Records: TStrings;Input: WideString);
  begin
    Records.Clear;
    Records.Delimiter := ';';
    Records.DelimitedText := Input;
  end;
var
List: TStrings;
begin
  List := TStringList.Create;
  try
    GetRecords(List,'leon,24,89,100;jimmy,30,77,56;');
    ListBox1.Items.Assign((list));//do here whatever you want with your record list
  finally
    List.Free;
  end;
end;
0
 
LVL 17

Expert Comment

by:mokule
ID: 10773385
uses
  StrUtils;

procedure TForm1.Extract(S: WideString);
var
  P,P1: integer;
begin
  P1 := 0;
  P := Pos(';',S);
  Memo1.Lines.BeginUpdate;
  while P > 0 do
    begin
    Memo1.Lines.Add(Copy(S,P1+1,P-P1-1));
    P1 := P;
    P := PosEx(';',S,P+1);
    end;
  if P1 < Length(S) then
    Memo1.Lines.Add(Copy(S,P1+1,Length(S)-P1));
  Memo1.Lines.EndUpdate;
end;
0
 
LVL 17

Expert Comment

by:mokule
ID: 10773500
And here is some test

my solution: 17665 ms

and

Feruccio: 671 ms

Well done.
0
 
LVL 2

Expert Comment

by:xxflip
ID: 10774349
This is why I love to read all the questions, once in a while I get my eyes opened ...

The best solution in my opinion: Ferruccio68
0
 
LVL 9

Accepted Solution

by:
mocarts earned 125 total points
ID: 10774891
Does TStrings (TStringList) supports widestring? if that is not so important anyway here is my solution ;)

const
  Semicolon: WideChar = ';';

function GetTotalRec(Input: WideString): integer;
var
  i: integer;
begin
  Result := 0;
  for i := 1 to Length(Input) do
    if Input[i] = Semicolon then
       inc(Result);
end;

function GetTotalRecPos(Input: WideString): TList;
var
  i: integer;
begin
  Result := TList.Create;
  try
    for i := 1 to Length(Input) do
      if Input[i] = Semicolon then
        Result.Add(pointer(i));
  except
    Result.Free;
    raise;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  p, i, TotalRec: integer;
  Input: WideString;
begin
  Input := WideString('leon,24,89,100;jimmy,30,77,56;');
  TotalRec := GetTotalRec(Input);
  with GetTotalRecPos(Input) do
  begin
    TotalRec := Count;
    p := 1;
    for i := 0 to TotalRec -1 do
    begin
      ShowMessage(Copy(Input, p, integer(List^[i]) - p));
      p := integer(List^[i])+1;
    end;
    Free;
  end;
end;

also you can split your string in records already in GetTotalRecXXX function
wbr, mo.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10775174
New unofficial test results
-----------------------------

my solution: 17665 ms
Feruccio: 671 ms
mocarts: 30ms

Well done.

It looks like I must improve time resolution (10ms now) or change tested data. ;-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10778958
I think Feruccio's and mocarts's solutions are equivalent each-other. They are the best !!!!

emil
0
 

Author Comment

by:coole
ID: 10789067
Thank you guys. Excellent work!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

12 Experts available now in Live!

Get 1:1 Help Now