reading seperate numbers from a line

I have the following line in a text file

numbers 500'200 100'600;

I want to be able to read both numbers in a separate edit box, but regardless of the position of the numbers and the length! so i want the same function to be able to read content regardless of the spaces  and the length of the numbers. The edit fields should load the numbers including with the single quote; say
Edit1= 500'200

numbers 50000'100       100'8000;

I do have a function which can read the first number  of a similar line but i can not read the second number, my current code is

Function test (Source : String ) : String;
var
 p1, p2:  Integer;
 temp: String;
begin
p1 := Pos( 'firstnumber',Source);
temp :=  Copy(Source, p1+12, Length(Source) );
temp := StringReplace ( temp, 'secondnumber', ';', []);
p2 :=Pos(';', temp);
if p2= 0 then
Result:= temp
else
Result := Copy (temp, 1, p2-1);
end;

This code reads the first number from a text file which has the content.
firstnumber 500'100 secondnumber 100'00;

i later call the function into the edit field with

Edit1.Text := Test( sl[0]);

How would i change my function to read both numbers, still ignoring the semicolon at the end as my actual code does and regardless of the length and posistion of the numbers? Hope question is clear enough. thanks!
itektasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
procedure test(Source: STring; Var First, Second: STring);
var
i: Integer;
begin
   Source := TrimLeft(Source);
   i := 1;
   while Source[i] <> ' ' do begin
      First := First+SOurce[i];
      inc(i);
   end;
   While Source[i] <> ';' do begin
      Second := Second+SOurce[i];
      inc(i);
   end;
   Second := TrimLeft(Second);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
S1,S2: STring;
begin
   Test(' 50000''100       100''8000;',S1,S2);
   Edit1.Text := s1;
   Edit2.Text := S2;
end;

F68 ;-)
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Sorry...i missed the firstnumber and secondnumber part....
Here's the correct one:

procedure test(Source: STring; Var First, Second: STring);
var
i: Integer;
begin
   SOurce := STringReplace(Source,'firstnumber','',[rfIgnoreCase]);
   SOurce := STringReplace(Source,'Secondnumber','',[rfIgnoreCase]);
   Source := TrimLeft(Source);
   i := 1;
   while Source[i] <> ' ' do begin
      First := First+SOurce[i];
      inc(i);
   end;
   While Source[i] <> ';' do begin
      Second := Second+SOurce[i];
      inc(i);
   end;
   Second := TrimLeft(Second);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
S1,S2: STring;
begin
   Test('firstnumber 500''100 secondnumber 100''00;',S1,S2);
   Edit1.Text := s1;
   Edit2.Text := S2;
end;

F68 ;-)

0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
btw i assume that your string have a final ; char. If not then the test procedure for secondnumber should be thie

...
...
while i <= Length(SOurce) do begin
      Second := Second+SOurce[i];
      inc(i);
   end;
   Second := TrimLeft(Second);
...
...
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

KumaoCommented:
function test(Source, Id: string): string;
var
  tmp: TStringList;
begin
  Result := '';
  tmp := TStringList.Create;
  try
    tmp.Delimiter := ';';
    tmp.DelimitedText := Source;
    Result := tmp[tmp.IndexOf(Id)+1];
  finally
    tmp.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := 'firstnumber 500''100   secondnumber 100''00;';
  Edit1.Text := test(s,'firstnumber');
  Edit2.Text := test(s,'secondnumber');
end;
0
kretzschmarCommented:
well,

my version

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  sl : TStringList;
  s  : String;
begin
  sl := TStringList.Create;
  s := edit1.Text;  //simulate input
  try
    //discard numbers
    splitstr(s,'numbers');
    //discard ;-char
    s := splitstr(s,';');
    //trimming
    s := trim(s);
    //extract needed content
    while s <> '' do
    begin
      sl.Add(trim(splitstr(s,' ')));
      //trimming
      s := trim(s);
    end;
    listbox1.Items.Assign(sl); //show result, can be also separated to edit-boxes
  finally
    sl.Free;
  end;
end;

end.

meikl ;-)
0
itektasAuthor Commented:
ok

kretzschmar I have tried your code but it doesnt seem to do what i was looking for, it seems to read the content of a text file into a listbox? and the editbox stays empty.  

 Ferruccio68 using your code i have changed mine to the following but it doesnt work yet i dont know why? shouldnt the procedure be a function? even when i do change it to a function i still cant compile it, i get an error that says " not enough parameters at the line "Edit1.Text := test( sl[0] );"  ok the code is as follows. Thanks

 procedure test(Source: STring; Var First, Second: STring);
var
i: Integer;
begin
   Source := TrimLeft(Source);
   i := 1;
   while Source[i] <> ' ' do begin
      First := First+Source[i];
      inc(i);
   end;
   While Source[i] <> ';' do begin
      Second := Second+Source[i];
      inc(i);
   end;
   Second := TrimLeft(Second);
end;

procedure TForm1.ReadFromFIle;

var
sl: TStringList;
temp: String;

begin
sl :=TStringList.Create;
sl.LoadFromFile('/home/test.txt');

Edit1.Text := test( sl[0] );
sl.free
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ReadFromFile;
end;
end.

0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Test now is a procedure, not a function, so you can't assign its result to edit1.text because it don't give a result...
as i posted above you must change the readfromfile as follows:

procedure TForm1.ReadFromFIle;
var
sl: TStringList;
temp1,Temp2: String;
begin
sl :=TStringList.Create;
sl.LoadFromFile('/home/test.txt');
test( sl[0],Temp1,Temp2); //this is a procedure . you must pass all parameters Source, First and Second
Edit1.Text := Temp1;
Edit2.Text := Temp2;
sl.free
end;
0
kretzschmarCommented:
>it seems to read the content of a text file into a listbox?

be a bit flexible,
the editbox, i took to simulate one file entry
the listbox, i took to show the the stringlist-content after processing

i guessed, you were able to replace this to your needs (seemed not so)

meikl ;-)
0
kretzschmarCommented:
maybe this looks better for u
(notice the slight chances)

unit itektas_u;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  sl : TStringList;
  s  : String;
begin
  sl := TStringList.Create;
  try
    //loading
    sl.loadFromFile('c:\itektas.txt');
    s := sl[0];
    sl.clear;
    //discard numbers
    splitstr(s,'numbers');
    //discard ;-char
    s := splitstr(s,';');
    //trimming
    s := trim(s);
    //extract needed content
    while s <> '' do
    begin
      sl.Add(trim(splitstr(s,' ')));
      //trimming
      s := trim(s);
    end;
    //show result
    edit1.text := sl[0];
    edit2.text := sl[1];
  finally
    sl.Free;
  end;
end;

end.

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
typo:
chances -> changes
0
itektasAuthor Commented:
kretzschmar that works very well now,
Ferruccio68 the code that you said works but its doing something different; for a text file that has the content

numbers 192'100 200'100;

it displays " range " in the first edit fields and 192'100 200'100 in the second edit field; I have tried to change it so that i can skip the second space and display the second number in an another edit field but i couldnt do it.

I will increase the points and split them
0
itektasAuthor Commented:
thanks a lot to all :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.