Recent

Author Topic: Operator "IN" is not overloaded – Simple question  (Read 1830 times)

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator "IN" is not overloaded – Simple question
« Reply #15 on: July 11, 2024, 01:00:34 pm »
However the thing that bothers me here is that while all this business about custom operators etc. demonstrates that Evolved Pascal and its more experienced users are remarkably versatile, it does little to help someone who I assume is a relatively inexperienced user- and nothing at all to dispel the myth that Pascal coding is afflicted by a grievous overload of boilerplate.
fwiw and imo the notion comes from the topic starter self by the way the question was asked. Somehow TS expects such a similar or look-a-like construct to work which might have been caused by/for several reasons (f.e. a language previously used that support such a syntax/construct).

We can ofc debate whether it is a good idea or not to oblige the requested syntax  :)

<Shrug> Looks fine to me, although I'd caution that that's not necessarily something that somebody brought up on a classical Pascal book would know about... and might not even be something he can locate in the RTL/FCL documentation.
There I believe you are mistaken. If you spend time looking for it then it is there, see here. I give you that you need to know where to look.

The cast to string is not necessary when the compiler is configured to use ansistrings (by default and as per my example).
All software is open source (as long as you can read assembler)

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator "IN" is not overloaded – Simple question
« Reply #16 on: July 11, 2024, 01:07:29 pm »
Sorry for the full quote but since it is on the previous page due to my late reply.....

Hi
Look in a thread with 'Stemplot' or something, I remember, 'cause I wrote a function in there as well...
edit: Found it: https://forum.lazarus.freepascal.org/index.php/topic,64745.0.html
I see now, it wasn't specifically for strings, but 'stem-records', sorry...
Please don't be sorry. If you have any idea how many threads are lingering in my brain (not only from this forum) then it probably is more likely for me to mess up. You (or at least I) can't remember every little detail and besides that it had to do with overloading operators so ... 90% accurate ?  :D

Thank you for your input/suggestions as it is appreciated.
All software is open source (as long as you can read assembler)

alpine

  • Hero Member
  • *****
  • Posts: 1253
Re: Operator "IN" is not overloaded – Simple question
« Reply #17 on: July 11, 2024, 01:17:18 pm »
Hi
Mark would this be better?
Code: Pascal  [Select][+][-]
  1. if not string('Peter, Paul, Mary').Contains('Paul') then exit;
  2.  
Regards Benny

Much better is:
Code: Pascal  [Select][+][-]
  1. if IndexStr('Paul',['Peter','Paul','Mary']) = -1 then exit;

The Pos() and String.Contains() here have the drawback of finding substrings, e.g. as in 'Pauline, Jean-Paul, etc.'
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Operator "IN" is not overloaded – Simple question
« Reply #18 on: July 11, 2024, 01:22:04 pm »
Hi
Yup @alpine, my money was on 'IndexStr' too, but you know, the more the marrier  :D
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: 7527
Re: Operator "IN" is not overloaded – Simple question
« Reply #19 on: July 11, 2024, 01:38:36 pm »
Yes, agreed. But a significant issue at this point is the extent to which OP wants to do a substring search vs an array-of-strings search, and the extent to which he's worked out the syntax for constant arrays of strings and so on.

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: 15555
  • Censorship about opinions does not belong here.
Re: Operator "IN" is not overloaded – Simple question
« Reply #20 on: July 11, 2024, 07:00:49 pm »
Not Morgan-Loyd, but Boyer-Moore.. :P O:-)
If I smell bad code it usually is bad code and that includes my own code.

emaza

  • Jr. Member
  • **
  • Posts: 59
    • http://GerenciaDeCondominios.com
Re: Operator "IN" is not overloaded – Simple question
« Reply #21 on: July 11, 2024, 09:36:40 pm »
Wow! Very grateful and totally overwhelmed by the number of quick knowledgeable replies. I Will need some time to read carefully and digest all this before reporting back. Of course I will continue to read and appreciate any further comments.   :D

emaza

  • Jr. Member
  • **
  • Posts: 59
    • http://GerenciaDeCondominios.com
Re: Operator "IN" is not overloaded – Simple question [SOLVED]
« Reply #22 on: August 01, 2024, 08:21:38 am »
Ok, it is never too late to say thank you so many thanks to all. My understanding,, now  hopefully correct and comments:

1, The In operator is for sets not strings.

2. Proper use of parenthesis, brackets, apostrophes and commas is to be learned but do not make a group of words a set, you must declare the set in the ‘Type’ section.

3. With proper declaration and syntax, the IN operator works on a SET of names and does not mistake Pauline for Paul.

4. Using POS on a String (if you search one String full of names and text) is simpler and requires no Type  declaration but will see Paul inside Oauline. Not a problem for me.

5. Using TStringList (….. ) should be similar to # 3, but my understanding was lacking, so I gave up on it for now.

6. Using “Strin X contained in String Z ? “ samer as #4 (Usung POS) though maybe there is a useful difference for some case…

7.   Although the IN operator is not for Strings, you can modify (Overload) it to work for Strings, if you know how. TRon knows how and gave the code.

8. The overload “function”: Operator In compiles fine but I cannot use it because I don’t know where to place it or how to call it or whatever must be done for it to run.

9. The following code, although NOT a sample of best practice programing might perhaps be of use to some lost soul like me, so I include it. -Will work if dropped in a 5-button form.

Code: Text  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Button3: TButton;
  18.     Button4: TButton;
  19.     Button5: TButton;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure Button2Click(Sender: TObject);
  22.     procedure Button3Click(Sender: TObject);
  23.     procedure Button4Click(Sender: TObject);
  24.     procedure Button5Click(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1   : TForm1;
  33.   Blind   : String = 'Error. Fails to see whats there ';
  34.   Good    : String = 'Good, it finds what is there ';
  35.   Mistake : String = 'Error. Thinks Pauline is Paul';
  36.   Perfect : String = 'Perfect: Does not see Paul in Pauline';
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. Type
  46.   Musicians = (Peter, Pauline, Paul, Mary);
  47.   // MonthRef = (01,03,12); // or similar will NOT work '01' not an identifier
  48.   //                        it is  an ordinal constatant
  49. var
  50.   Performer : Musicians;
  51.  
  52.   begin
  53.     ShowMessage('Using Set & Type' +Chr(13)
  54.               +'Note: not TStringList . IndexStr');
  55.  
  56.  
  57.     If Pauline in ([Peter, Pauline, Mary]) Then
  58.                  ShowMessage(Good) else ShowMessage(Blind);
  59.  
  60.   Performer := Paul;
  61.   If NOT( Performer in ([Peter,Pauline,Mary])) Then
  62.                  ShowMessage(Perfect) else ShowMessage(Mistake);;
  63.   end;
  64.  
  65.  
  66. procedure TForm1.Button2Click(Sender: TObject);
  67. begin
  68.   ShowMessage('Usimg POS');
  69.   if pos('Pauline','Peter, Pauline, Mary') = 0 then
  70.                showMessage(Blind) else showMessage(Good);
  71.  
  72.   If pos('Paul','Peter, Pauline, Mary') = 0 then
  73.                showMessage(Perfect) else showMessage(Mistake);
  74. end;
  75.  
  76. procedure TForm1.Button3Click(Sender: TObject);
  77. begin
  78.   ShowMessage('Testing Contains option');
  79.   if not string('Peter, Paul, Mary').Contains('Paul') then
  80.             ShowMessage(Blind) else ShowMessage(Good);
  81.   If NOT String('Peter, Pauline, Mary'). CONTAINS ('Paul') then
  82.             ShowMessage(Perfect) else ShowMessage(Mistake);
  83. end;
  84.  
  85. procedure TForm1.Button4Click(Sender: TObject);
  86. Type
  87.    Musicians = TStringList;
  88. Var
  89.   BandMembers: Musicians;
  90.   //Performer : String;
  91. begin
  92.   ShowMessage('Sorry, TStrngList test abandonrd');
  93.   Exit ;
  94.  
  95.   { ********  ACCESS VIOLATION & OTHER ERRORS  ***************
  96.  
  97.   //BandMembers:= ['Peter';,'Pauline', 'Mary']erformer := 'Paul';
  98.   BandMembers.Clear;
  99.   BandMembers.Add('Peter');
  100.    BandMembers.Add('Pauline');
  101.    BandMembers.Add('Mary');
  102.  
  103.  
  104.    //IndexStr('Pauline',[Bandmembers]) = -1 then
  105.    If Bandmembers.IndexOf('Pauline') = -1 then
  106.                          ShowMessage(Blind) else ShowMessage(Good);
  107.  
  108.    //If IndexStr('Pauline',['Peter','Paul','Mary']) = -1 then
  109.    If BandMembers.IndexOf('Pau') = -1 then
  110.              ShowMessage(Perfect) elde ShowMessage(Mistake);
  111.  
  112.   }
  113. end;
  114.  
  115.  
  116. operator in (const aString: string; const aArray: array of string): boolean;
  117. var
  118.   s: string;
  119. begin
  120.   result := false;
  121.   for s in aArray do
  122.     if s = aString then exit(true);
  123. end;
  124.  
  125. procedure TForm1.Button5Click(Sender: TObject);
  126. begin
  127.   ShowMessage('If IN operator overloaded correctly, this should say hello');
  128.    if 'paul' in TStringArray.Create('Jason', 'Paul', 'John')
  129.       then ShowMessage('hello') ELSE ShowMessage('Noy working');;
  130. end;
  131.  
  132. end.
  133.                            
  134.  
  135.  

10. Since some wondered what exactly was I doing:  The data contains a 6-digit integer field that is a date reference: first 4 is year, last two is month (but why? Well, because it does) to keep garbage out and to present to humans, the program converts the integer to a String, copies the last two characters and before accepting the input checks them to be 01 or 02 etc. I had forgotten this was already done in another procedure reconverting the two-character ending to an integer and checking its value -now I can use POS instead.  Sorry. By trying to keep the question short and simple I presented a more difficult one.

Considering all of the above I have marked the topic CLOSED, but abusing your patience and hoping to get some free tutoring:
a.   Ref # 3 above: Lazarus does not allow defining a set  Type  MonthRef = (01,03,12) or ([‘01’, ‘02’, ‘11’)] etc Which would be useful to me for reasons in # 10 above. Solution?
b.   How to make “Operator In” run.
       
My apologies if I missed an important part of received suggestions. Nice day to all.

« Last Edit: August 01, 2024, 08:28:46 am by emaza »

 

TinyPortal © 2005-2018