Solved

reading seperate numbers from a line

Posted on 2003-11-13
12
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:Kumao
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 30 total points
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
typo:
chances -> changes
0
 

Author Comment

by:itektas
Comment Utility
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
Comment Utility
thanks a lot to all :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

6 Experts available now in Live!

Get 1:1 Help Now