Solved

reading seperate numbers from a line

Posted on 2003-11-13
12
248 Views
Last Modified: 2010-04-05
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!
0
Comment
Question by:itektas
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 9744452
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 9744496
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 9744515
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Expert Comment

by:Kumao
ID: 9744847
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9745842
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
 

Author Comment

by:itektas
ID: 9752500
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
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 30 total points
ID: 9753122
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9753218
>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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 30 total points
ID: 9753252
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9753258
typo:
chances -> changes
0
 

Author Comment

by:itektas
ID: 9754849
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
 

Author Comment

by:itektas
ID: 9754856
thanks a lot to all :)
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 157
FMX StringGrid1->Canvas->FillRect Problem 3 153
Performance of SQL statement 37 111
Firemonkey DbLookupComboBox equivalent ? 2 47
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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