Forum > General

Operator "IN" is not overloaded – Simple question

<< < (2/5) > >>

cdbc:
Hi
--- Quote ---Will add example later if it has not already been done.
--- End quote ---
@Thaddy: I think @TRon wrote one a few months ago... It's here in the forum...
Regards Benny

TRon:

--- Quote from: cdbc on July 11, 2024, 10:25:32 am ---@Thaddy: I think @TRon wrote one a few months ago... It's here in the forum...

--- End quote ---
Strange, as I can't seem to recall that (but my brain is mush)  :D

I did wrote a reverse integer in operator for boleeman a few months back though.

Nevertheless:

--- Code: Pascal  [+][-]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";}};} ---uses  sysutils; 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;   if 'paul' in TStringArray.Create('Jason', 'Paul', 'John')   then writeln('hello'); 
And there you can see that your previous suggestions of using something like IndexStr or Pos or even

--- Code: Pascal  [+][-]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";}};} ---  if 'paul'.IndexOfAny(['Peter', 'Paul', 'Mary']) = -1   then writeln('not in')    else writeln('in'); 
Is just as easy as using the in operator because when using the operator there is still need to create the (typed) array.

So your first thought was a good one cdbc  :)

Zvoni:
Everything said above is true

OP's Problem is here

--- Quote --- If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;     
Will not work.
--- End quote ---
He looks for 'Paul' inside a String 'Peter, Paul, Mary' --> that's a simple string, so Mark's idea with Pos is actually the "go to"-solution

Any other of above proposed solutions imply Sets or Arrays, even the first 2 cases of OP's first post uses sets/Arrays

His "mistake" is in thinking 'Peter, Paul, Mary' is a Set/Array

He mentioned "assembling" the String.
Maybe if he showed this "assembling"-code we could point him directly to the solution, code-snippets above not withstanding

Thaddy:
Well, the basics are easy, and since Tron seems to say he did not write it, I will write it..

cdbc:
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...
Regards Benny

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version