Recent

Author Topic: [Solved] A Tip for beginners  (Read 4007 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 95
[Solved] A Tip for beginners
« on: January 17, 2025, 11:10:10 pm »
Just a reminder that when iterating over a string and inserting or deleting characters you have
to use "Downto" not "To" to get your expected results when processing multiple characters.

In this case below the expected result is the removal of all '4' characters from the string.
ie. 'AB4C44DE' becomes ABCDE'.

I leave it as an exercise for the newbie to understand why this is so.

P.S. Yes; I know that there are a myriad of ways to attack this problem. This example is for newbies

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: string;
  4.   x: integer;
  5. begin
  6.  
  7.   S := '412345446444474';
  8.   for x := 1 to length(S) do if s[x] = '4' then Delete(S, x, 1);
  9.   Memo1.Lines.add(S); //Result '123546447' is incorrect
  10.  
  11.   S := '412345446444474';
  12.   for x := Length(S) downto 1 do if s[x] = '4' then Delete(S, x, 1);
  13.   Memo1.Lines.add(S); //Result '123567' is correct
  14. end;                
« Last Edit: January 20, 2025, 03:55:41 pm by Ten_Mile_Hike »
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: A Tip for beginners
« Reply #1 on: January 18, 2025, 10:10:04 am »
It's better to remind beginners to turn on checks (at least -Ciro) and use debugger while developing, because the line
Code: Pascal  [Select][+][-]
  1. for x := 1 to length(S) do if s[x] = '4' then Delete(S, x, 1);

raises Range check error.

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #2 on: January 18, 2025, 10:55:56 am »
@tetrastes
You are not a beginner. Downto is better. Do you really want me to give you the answer?  ;)
But I am sure they don't want the Trumps back...

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: A Tip for beginners
« Reply #3 on: January 18, 2025, 11:25:04 am »
Of course downto is better, as to is simply buggy. ;) And the answer is obvious after seeing range check error, particularly for a beginner.
Are you against using checks and debugger?

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #4 on: January 18, 2025, 11:36:30 am »
No, I am against re-allocations... ;D
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1871
    • http://www.cdbc.dk
Re: A Tip for beginners
« Reply #5 on: January 18, 2025, 12:02:04 pm »
Hi
@tetrastes:
Quote
downto is better, as to is simply buggy.
NO 'to' is not buggy!!!
The reason you can't use 'to' in that situation, is because you're deleting char(s) as you go, thereby changing the count of items, which in turn screws up your loop!!!
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

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: A Tip for beginners
« Reply #6 on: January 18, 2025, 12:26:49 pm »
@cdbc
If we can say "downto is better", why we can't say "to is buggy"?  :D
I think it was obvious, what these meant. Why deprive newbies of finding the answer themselves?

dbannon

  • Hero Member
  • *****
  • Posts: 3230
    • tomboy-ng, a rewrite of the classic Tomboy
Re: A Tip for beginners
« Reply #7 on: January 18, 2025, 12:38:05 pm »
If we can say "downto is better", why we can't say "to is buggy"?  :D
  • Because its not buggy. It does exactly what it is designed and documented to do.
  • Because labelling something as basic as that as buggy is bad for FPC image.


(I am not suggesting we should not call out real bugs for that reason !)

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

cdbc

  • Hero Member
  • *****
  • Posts: 1871
    • http://www.cdbc.dk
Re: A Tip for beginners
« Reply #8 on: January 18, 2025, 12:42:06 pm »
Thanks Davo, exactly my thoughts  ;)
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

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: A Tip for beginners
« Reply #9 on: January 18, 2025, 12:51:23 pm »
It's strange to me that someone understand this phrase verbatim. I think its meaning was clear in the context of discussion.
Of course I don't think that "to is buggy" by itself. And that "downto is better" always and everywhere.  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #10 on: January 18, 2025, 02:17:28 pm »
@ tetrastes

You still don't get it, do you?
Removing chars with downto does not re-allocate memory for the whole string.
It just performs a move on the string tail. There is no need for reallocmem. It shrinks.
Removing chars with to, from zero,  DOES re-allocate memory for the whole string every time and is therefor considerably slower, calls reallocmem/getmem, move and freemem on every occasion.
So much for the beginners!
There, I gave it away. >:D
Basics, basics, basics. O:-)

It was a genuinely good question for beginners and then an old hand falls in the same trap and forces me to reveal the answer.

Shame on you  ::)

(at least the beginners now have the explanation)

Otherwise it is my opinion that to is not buggy. (who invented that novelty?)
Actually that is fact, not opinion, but I was watching a documentary about Carthage, "Ceterum censeo Carthaginem esse delendam,"  %).
« Last Edit: January 18, 2025, 02:51:02 pm by Thaddy »
But I am sure they don't want the Trumps back...

MarkMLl

  • Hero Member
  • *****
  • Posts: 8234
Re: A Tip for beginners
« Reply #11 on: January 18, 2025, 03:10:02 pm »
It's strange to me that someone understand this phrase verbatim. I think its meaning was clear in the context of discussion.
Of course I don't think that "to is buggy" by itself. And that "downto is better" always and everywhere.  :D

In that case choose your words better: "to is /inappropriate/ in this case" is the correct way of putting it, particularly in something which is labelled as being for beginners.

However I'd also point out that since we're discussing strings here, with no indication of the encoding, it's necessary to take into account that if UTF-8 characters are involved the result might still be unexpected.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #12 on: January 18, 2025, 03:15:26 pm »
Oh, Mark,
He has no clue anyway, why bother to overload his brain? :'(
But I am sure they don't want the Trumps back...

LV

  • Full Member
  • ***
  • Posts: 219
Re: A Tip for beginners
« Reply #13 on: January 18, 2025, 04:47:53 pm »
This is excellent advice. But this

Code: Pascal  [Select][+][-]
  1.   S := '412345446444474';
  2.   for x := 1 to length(S) do if s[x] = '4' then Delete(S, x, 1);
  3.   Memo1.Lines.add(S); //Result '123546447' is incorrect
  4.  

is usually called a beginner programmer's mistake, not an FPC bug. Isn't it?

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: A Tip for beginners
« Reply #14 on: January 18, 2025, 04:50:31 pm »
@Thaddy

Though you like to insult people, I'll still answer you.

1. I didn't give any answer on OP's question, so why "shame on me"?  :D

2. Your answer explaines nothing, though it contains much bragging, as usual. The code with "to" doesn't work not because of reallocations and other stuff (which is problem of inefficiency only). If there was implementation without reallocation, it still wouldn't work, because it is mathematically incorrect. And I won't write more, let beginners think themselves (though @cdbc gave the clue).

But to illustrate this:
Code: Pascal  [Select][+][-]
  1.   S := '412345446444474';
  2.   x := 1;
  3.   repeat
  4.       if s[x] = '4' then Delete(S, x, 1)
  5.       else Inc(x);
  6.   until x = length(S)+1;
This code works despite all reallocations, etc.

 

TinyPortal © 2005-2018