Forum > General

Operator "IN" is not overloaded – Simple question

<< < (5/5)

Thaddy:
Not Morgan-Loyd, but Boyer-Moore.. :P O:-)

emaza:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    Button4: TButton;    Button5: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);    procedure Button5Click(Sender: TObject);  private   public   end; var  Form1   : TForm1;  Blind   : String = 'Error. Fails to see whats there ';  Good    : String = 'Good, it finds what is there ';  Mistake : String = 'Error. Thinks Pauline is Paul';  Perfect : String = 'Perfect: Does not see Paul in Pauline'; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);Type  Musicians = (Peter, Pauline, Paul, Mary);  // MonthRef = (01,03,12); // or similar will NOT work '01' not an identifier  //                        it is  an ordinal constatantvar  Performer : Musicians;   begin    ShowMessage('Using Set & Type' +Chr(13)              +'Note: not TStringList . IndexStr');      If Pauline in ([Peter, Pauline, Mary]) Then                 ShowMessage(Good) else ShowMessage(Blind);   Performer := Paul;  If NOT( Performer in ([Peter,Pauline,Mary])) Then                 ShowMessage(Perfect) else ShowMessage(Mistake);;  end;  procedure TForm1.Button2Click(Sender: TObject);begin  ShowMessage('Usimg POS');  if pos('Pauline','Peter, Pauline, Mary') = 0 then               showMessage(Blind) else showMessage(Good);   If pos('Paul','Peter, Pauline, Mary') = 0 then               showMessage(Perfect) else showMessage(Mistake);end; procedure TForm1.Button3Click(Sender: TObject);begin  ShowMessage('Testing Contains option');  if not string('Peter, Paul, Mary').Contains('Paul') then            ShowMessage(Blind) else ShowMessage(Good);  If NOT String('Peter, Pauline, Mary'). CONTAINS ('Paul') then            ShowMessage(Perfect) else ShowMessage(Mistake);end; procedure TForm1.Button4Click(Sender: TObject);Type   Musicians = TStringList;Var  BandMembers: Musicians;  //Performer : String;begin  ShowMessage('Sorry, TStrngList test abandonrd');  Exit ;   { ********  ACCESS VIOLATION & OTHER ERRORS  ***************   //BandMembers:= ['Peter';,'Pauline', 'Mary']erformer := 'Paul';  BandMembers.Clear;  BandMembers.Add('Peter');   BandMembers.Add('Pauline');   BandMembers.Add('Mary');     //IndexStr('Pauline',[Bandmembers]) = -1 then   If Bandmembers.IndexOf('Pauline') = -1 then                         ShowMessage(Blind) else ShowMessage(Good);    //If IndexStr('Pauline',['Peter','Paul','Mary']) = -1 then   If BandMembers.IndexOf('Pau') = -1 then             ShowMessage(Perfect) elde ShowMessage(Mistake);   }end;  operator in (const aString: string; const aArray: array of string): boolean;var  s: string;begin  result := false;  for s in aArray do    if s = aString then exit(true);end; procedure TForm1.Button5Click(Sender: TObject);begin  ShowMessage('If IN operator overloaded correctly, this should say hello');   if 'paul' in TStringArray.Create('Jason', 'Paul', 'John')      then ShowMessage('hello') ELSE ShowMessage('Noy working');;end; end.                             
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.

Navigation

[0] Message Index

[*] Previous page

Go to full version