Solved

Using 'in' for an Array, not a Set

Posted on 2004-04-05
4
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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