Solved

Using 'in' for an Array, not a Set

Posted on 2004-04-05
4
169 Views
Last Modified: 2010-04-05
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
Comment
Question by:MartinC
  • 2
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10763370
defintivly no!

you need a function which loops through the array

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10763374
well, i need no points :-)))
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 60 total points
ID: 10763444
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
 

Author Comment

by:MartinC
ID: 10763922
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now