Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Values accumelate automaticly??

Status
Not open for further replies.

RobPouwelse

Technical User
Nov 19, 2001
77
NL
Next one :)..
I'll try and explain what's the problem here....

First you got the old FUNCTION
================================================
Code:
function Func_from (Str1, Str2 :string):string ;
var index, StrLength : integer;

begin
index := pos( Str1, Str2 ) + 5;
if index <> 0 then
begin
StrLength := pos( '>, size', Str2 ) - index;
Str1 := copy( Str2, index+1, strLength-1 );
end;
Result:=Str;
end;
=================================================
Problem with this is that although i made an IF statement it would never work cause the index would pretty much always be > 0 (5 to be exact). So i changed it into this :
================================================
Code:
index := pos( Str1, Str2 );
if index <> 0 then
begin
StrLength := pos( '>, size', Str2  ) - (index+5);
etc.
=================================================
If i'm correct, this would do the trick.. now index can be 0 before the statement.

Then i changed the function into a procedure cause of the &quot;bad coding&quot; post. (tnx for the info m8) so now it's this :
=================================================
Code:
Procedure TForm1.Proc_from (var Str1, Str2 :string);
var index, StrLength : integer;

begin
index := pos(Str1, Str2);
if index <> 0 then
begin
StrLength := pos( '>, size', Str2  ) - (index+5);
Str1 := copy( Str2, index+1, StrLength-1 );
if Str1='' then
else Memo1.Lines.Append(Str1);
end;
end;
==================================================
I made it a TForm cause of the Memo1 append.
Now the problem is that Index does constantly increase in value as does StrLength constantly decrease in value..
What i don't understand is, i don't have a specific line like index:=index+1; and/or Strlength:=Strlength-1;

So is the (index+5) in
Code:
StrLength := pos( '>, size', Str2  ) - (index+5);
permanent? why?.. do i have to do this??
=================================================
Code:
Procedure TForm1.Proc_from (var Str1, Str2 :string);
var index, StrLength : integer;

begin
index := pos(Str1, Str2);
if index <> 0 then
begin
StrLength := pos( '>, size', Str2  ) - (index+5);
Str1 := copy( Str2, index+1, StrLength-1 );
index:=index-1;
StrLength:=StrLength+1;
if Str1='' then
else Memo1.Lines.Append(Str1);
end;
end;
==================================================

Sorry for the long post, (and for ANOTHER one on pretty much the same subject).. oh, and i'm sorry about the bad english :):)
 
Hi RobPouwelse

I think you need to review exactly what the procedure is doing.
Some points to watch / Note.

You can use inc(X); and/or Dec(x); (neater that X := X + 1;)

Don't forget that pos(s1,s2) gives the first character an index of 1 NOT 0.

Simularly
Copy (S1,0,1) and Copy(s1,1,1) will return the same character.

Your procedure does not change Str2 so you don't have to make that a 'var' parameter.

Your final version of the procedure dos not use the new values of 'index' and 'StrLen' anyway ?

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top