Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Using 'in' for an Array, not a Set

Is it possible to use the 'in' operator to find if an integer is in an array of integer?

The boolean-resulting:

7 in [5, 7, 9]

... would return TRUE for instance. Can I do something similar like:

7 in aNumbers

where aNumbers is an array of integer? Or do I have to do an ugly looping checker against each aNumbers[x] from 0 to length(aNumbers)?

Please give reasons for your answer: no points for just the quickest person to type "nope"   ;-)

Martin
0
MartinC
Asked:
MartinC
  • 2
1 Solution
 
kretzschmarCommented:
defintivly no!

you need a function which loops through the array

meikl ;-)
0
 
kretzschmarCommented:
well, i need no points :-)))
0
 
ZhaawZSoftware DeveloperCommented:
If working with strings, it could be AnsiMatchText() (case insensitive) or AnsiMatchStr() (case sensitive) from StrUtils.
Looping through array is not so ugly:

function IntegerInArray (number : integer; const arr : array of integer) : boolean;
var n : integer;
begin
for n := low(arr) to high(arr) do if arr[n] = number then break;
result := n <= high(arr);
end;
0
 
MartinCAuthor Commented:
Ah well, if Kretzschmar The Infallible just says "nope" i guess I have to accept it ... thanks for promptness! Given that Kretschmar has said he doesn't need the points and Zhaawh has saved me even writing the function, and i am an incredibly lazy person, i'm gonna give the points to Zhaawh.

Thanks to both!

Martin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now