Solved

Using 'in' for an Array, not a Set

Posted on 2004-04-05
4
170 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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