Recent

Author Topic: Length of String is not fixed inside For?  (Read 3017 times)

d2010

  • Full Member
  • ***
  • Posts: 114
Length of String is not fixed inside For?
« on: February 10, 2025, 07:06:34 am »
Hello fun
The main problem is not Length(String) , main reason is For.
The function "For" accept value not fixed ??
the 01 is fixed , but the "Length(rezultat)" is not fixed.
How to force for accept fix of Length(rezultat)?

Code: [Select]
Uses Windows;
 {$MODE Delphi}
Var rezultat:string;
function php_addslashes(dimlwd:integer):truebool;
var nth,skio:integer; old:char;
begin result:=false;old:=' ';
             if (dimlwd<1) then exit else result:=true;
      skio;=00;       
      nth:=000;  //inside   Virtual PascaL, For do not initialize the variabile nth
                   // You must reset  value of Nth
      for nth:=01 to Length(rezultat) do if (nth>skio) then
       Begin  if(rezultat[nth]='\') anbd (old<>'\') then
                 Begin system.insert('\',rezultat,nth);
                       skio:=nth+02;
                 End else old:=rezultat[nth];
        End;
End;

Begin
  php_addslashes(1050);
End.
« Last Edit: February 10, 2025, 07:15:53 am by d2010 »

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: Length of String is not fixed inside For?
« Reply #1 on: February 10, 2025, 07:17:43 am »
Use a while..do or repeat..until loop instead of for..to, in which case do not forget to increase the index (nth) manually.
Today is tomorrow's yesterday.

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: Length of String is not fixed inside For?
« Reply #2 on: February 10, 2025, 08:12:35 am »
Oh, strike that previous answer  :-[

I thought you wanted your string to grow to make room for the back-slashes but seems you want it the other way around.

Well, the for loop is iterated exactly 1 to length(rezultat) times. But, the loop inserts characters when certain conditions are met so the string will grow and does so beyond the maximal value of length(rezultat). If you have many back-slashes in the original string then you are able to see that the original string gets truncated (but at the same time will grow in length with the  amount of back-slashes inserted).

Either set the length back to the initial length with SetLenght or delete the last character from the string each time an insert takes place.

Today is tomorrow's yesterday.

Zvoni

  • Hero Member
  • *****
  • Posts: 2895
Re: Length of String is not fixed inside For?
« Reply #3 on: February 10, 2025, 08:18:41 am »
i'm trying to make heads or tails out of that code.

he passes 1050 for "dimlwd", but doesn't use that var/argument anywhere after entering the function.

Next: Above code shouldn't even compile, since he has a typo in his most inner If-Clause ("anbd")

Going by the name of the Function, he's trying to "add slashes", and i'm guessing, he's trying to "duplicate" them, so why not just use StringReplace?

btw: running through a List, String, array, whatever, where you want to change something (Delete or Insert), which in itself changes the Count:
Run backwards through it......
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: Length of String is not fixed inside For?
« Reply #4 on: February 10, 2025, 09:16:33 am »
Well, I initially interpreted the code and question wrong.

We already know but OP seems to have missed the fact that adding characters to a string is going to increase the length the string which in this particular case seem to have lead to the wrongful conclusion that the for loop isn't using a fixed endpoint (to-value).

The original code produces something like:
Code: [Select]
I \always\ arrive late at the office, but I make up for it by leaving \early\.
len(rezultat) = 78
iterations = 78
I \\always\\ arrive late at the office, but I make up for it by leaving \\early\.
len(rezultat) = 81
... and which seem to have confused OP in believing that the for loop is increasing its to-value check during the loop  (which is the wrong conclusion, see the number of iterations which is exactly matching the original length of the string)

But, the string did increase in length from 78 to 81 characters and this is because there was a total of 3 back-slashes added to the string. 81 - 78 = 3 (or 78 + 3 = 81, same numbers)

OP seems to be looking (I have to guess a little) for a solution that does:
Code: [Select]
I \always\ arrive late at the office, but I make up for it by leaving \early\.
len(rezultat) = 78
iterations = 78
I \\always\\ arrive late at the office, but I make up for it by leaving \\earl
len(rezultat) = 78
Which deletes as many characters from the string (from the end) as there are inserted. Number of iterations stays 78 but also the length of the resulting string stays the same.

Beats me why someone would be looking for such functionality, but I often do no have the foggiest about such things  :)
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8306
Re: Length of String is not fixed inside For?
« Reply #5 on: February 10, 2025, 09:27:51 am »
Well, I initially interpreted the code and question wrong.

We already know but OP seems to have missed the fact that adding characters to a string is going to increase the length the string which in this particular case seem to have lead to the wrongful conclusion that the for loop isn't using a fixed endpoint (to-value).

Actually, I think what he wanted to ask is "Termination condition is not fixed at the start of  for  ?".

Leaving aside the fact that his code as given wouldn't work, but I think he has justification being confused: Pascal /does/ accept a non-constant expression in that situation and arguably /shouldn't/.

Quote
How to force for accept fix of Length(rezultat)?

Regretably, you can't.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2895
Re: Length of String is not fixed inside For?
« Reply #6 on: February 10, 2025, 09:28:45 am »
So he's trying to add "Escape"-Slashes...

Again: Why not just use StringReplace?

Code: Pascal  [Select][+][-]
  1. Uses SysUtils;
  2. ....
  3. rezultat:=StringReplace(rezultat, '\', '\\',[rfReplaceAll]);
  4.  
Could even use the second overload, and check how many have been replaced
https://www.freepascal.org/docs-html/rtl/sysutils/stringreplace.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2895
Re: Length of String is not fixed inside For?
« Reply #7 on: February 10, 2025, 09:32:09 am »
Quote
How to force for accept fix of Length(rezultat)?

Regretably, you can't.

MarkMLl
Mark, leaving aside the initial intention of what TS wants to do:
The fact that the bounderies of a For-Loop are cached at entrance has been discussed (and proven) ad nauseum.
a simple forum-search would have revealed that

And i'm still not sure, what the initial intention of TS is.

If he really wants to "duplicate" all slashes, but keep the original length of the String....
Code: Pascal  [Select][+][-]
  1. Uses Sysutils;
  2. Var l:Integer;
  3. ......
  4. l:=Length(rezultat);  //Original length
  5. rezultat:=StringReplace(rezultat, '\', '\\', [rfReplaceAll]);  //Replace all Slashes with DoubleSlash
  6. rezultat:=LeftStr(rezultat, l);  //Cut the String back to original Length
« Last Edit: February 10, 2025, 09:36:35 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: Length of String is not fixed inside For?
« Reply #8 on: February 10, 2025, 09:54:19 am »
Actually, I think what he wanted to ask is "Termination condition is not fixed at the start of  for  ?".
Hmz, you got a point there. Something I do not even think about anymore as it has become an automatism to accept it as it is but indeed might perhaps have been the reason for OP to post.

There is only one person able to clear that up for us  :)

So he's trying to add "Escape"-Slashes...
I believe so, yes.

Quote
Again: Why not just use StringReplace?
Guessing again: a) virtual pascal does not have a StringReplace function (can't remember if TP has it) ? and/or b) it replaces all the slashes thereby increasing the length of the string.

Depending on what OP actually meant with the words "the 01 is fixed , but the "Length(rezultat)" is not fixed" and "How to force for accept fix of Length(rezultat)?" b might be applicable (or not) but I do not know about a (unable to locate a Linux version of virtual pascal in order to try/test).

Questions, riddles and more questions  :)

Please help us out d2010 !
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 1933
    • http://www.cdbc.dk
Re: Length of String is not fixed inside For?
« Reply #9 on: February 10, 2025, 09:54:27 am »
Hi
Hmmm, if not stringreplace, then maybe like this:
Code: Pascal  [Select][+][-]
  1. function PhpAddSlashes(const aStr: ansistring): ansistring;
  2. var li, lr: integer; ch: char; cnt: cardinal = 0;
  3. begin
  4.   if aStr = '' then exit(aStr);
  5.   SetLength(Result,Length(aStr)*2); { make room for an extra '/' for each char }
  6.   lr:= 1; { local result idx }
  7.   for li:= 1 to Length(aStr) do begin { run local idx through the string }
  8.     ch:= aStr[li];
  9.     if ch = '/' then begin
  10.       Result[lr]:= '/'; { first insert an extra '/' @ this position }
  11.       inc(lr); { increment result idx }
  12.       inc(cnt); { keep track of insertions }
  13.     end;
  14.     Result[lr]:= ch; { for all chars, copy the actual char to result }
  15.     inc(lr); { increment result idx }
  16.   end;
  17.   SetLength(Result,Length(aStr) + cnt); { now set the actual length for real }
  18. end;
I dunno, but like this it makes some sense to me ...at least.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 8306
Re: Length of String is not fixed inside For?
« Reply #10 on: February 10, 2025, 09:55:11 am »
Quote
How to force for accept fix of Length(rezultat)?

Regretably, you can't.

MarkMLl
Mark, leaving aside the initial intention of what TS wants to do:
The fact that the bounderies of a For-Loop are cached at entrance has been discussed (and proven) ad nauseum.

Yes, I know. OP doesn't.

From his phrasing, I read it that he realises that his code is broken but is asking how the compiler can be told that the termination condition /must/ be fixed, i.e. that using Length() in that context is an error since it doesn't return a constant.

I don't think he is asking how the compiler can be forced to reevaluate Length() on each iteration.

In both cases, the answer is "it can't".

Updated: for the sake of clarity:

* Free Pascal always calculates the upper bound exactly once before initializing the counter variable with the initial value.

* It is not allowed to change (i. e. assign a value to) the value of a loop variable inside the loop.

https://www.freepascal.org/docs-html/current/ref/refsu58.html#x168-19200013.2.4 with the reminder that current documentation can be reached from https://www.freepascal.org/docs.html

MarkMLl
« Last Edit: February 10, 2025, 10:00:17 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2895
Re: Length of String is not fixed inside For?
« Reply #11 on: February 10, 2025, 09:57:21 am »
Quote
Again: Why not just use StringReplace?
Guessing again: a) virtual pascal does not have a StringReplace function (can't remember if TP has it) ? and/or b) it replaces all the slashes thereby increasing the length of the string.
WTF is "Virtual Pascal"?

If there really is no StringReplace, then walk backwards through the String and be done with it.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8306
Re: Length of String is not fixed inside For?
« Reply #12 on: February 10, 2025, 10:01:44 am »
WTF is "Virtual Pascal"?

https://en.wikipedia.org/wiki/Virtual_Pascal

It might surprise you, but there's more than one kid on the block...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10897
  • Debugger - SynEdit - and more
    • wiki
Re: Length of String is not fixed inside For?
« Reply #13 on: February 10, 2025, 10:02:25 am »
The function "For" accept value not fixed ??
the 01 is fixed , but the "Length(rezultat)" is not fixed.
"FOR" evaluates the start and end value before the loop, and then does not change/update that anymore.

Your "length(rezultat)" is taken before the loop starts, and then the value is remembered (cached).
That is documented.

See example below, it will output from 1 to 5. Even though, that "val" will change to 10.
It only matters that "val" was 5 when the loop started.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   i, val: integer;
  4. begin
  5.   val := 5;
  6.   for i := 1 to val do begin
  7.     writeln(i);
  8.     val := 10;
  9.   end;
  10.   readln;
  11. end.
  12.  

As said by others, use "while" or "repeat" if you need a changing end-point.

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: Length of String is not fixed inside For?
« Reply #14 on: February 10, 2025, 10:03:43 am »
WTF is "Virtual Pascal"?
Wikipedia and referring to it because the comments in the code mentions it.

Quote
If there really is no StringReplace, then walk backwards through the String and be done with it.
Yes, I know.

fwiw instead of just dumping some code, I wanted to try make this a learning moment for OP. Problem right now is what needs to be taught because that seems unclear atm :)
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018