473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

checking for any value in an array

let's say I have a string that can be any number of items comma delimited:

2, 4, 6, 12, 345

I want to check against those values in an if/then statement:

if integer = any value in the string, then...

It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out

if [my value] = [any item in the array collection] then...

or do I need to loop through each array item one by one looking for a match?

-Darrel
Nov 19 '05 #1
11 1719
Yes, the only way is to convert the string to an integer array using a split
and search inside the array. If your array is very large, you might want to
consider sorting it and doing a binary search for the value to reduce your
search space.

Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.

Hope this helps
--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
let's say I have a string that can be any number of items comma delimited:

2, 4, 6, 12, 345

I want to check against those values in an if/then statement:

if integer = any value in the string, then...

It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out

if [my value] = [any item in the array collection] then...

or do I need to loop through each array item one by one looking for a
match?

-Darrel

Nov 19 '05 #2
Yes, the only way is to convert the string to an integer array using a split
and search inside the array. If your array is very large, you might want to
consider sorting it and doing a binary search for the value to reduce your
search space.

Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.

Hope this helps
--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
let's say I have a string that can be any number of items comma delimited:

2, 4, 6, 12, 345

I want to check against those values in an if/then statement:

if integer = any value in the string, then...

It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out

if [my value] = [any item in the array collection] then...

or do I need to loop through each array item one by one looking for a
match?

-Darrel

Nov 19 '05 #3
"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
or do I need to loop through each array item one by one looking for a
match?


Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.
Nov 19 '05 #4
"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
or do I need to loop through each array item one by one looking for a
match?


Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.
Nov 19 '05 #5
> Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.


Hmm...it does seem like a hack, but, at the same time, seems more efficient.
It's already a text string to begin with, and with this, I'm only searching
once, instead of looping through it all.

Not that I don't need to learn arrays, of course. ;o)

-Darrel
Nov 19 '05 #6
> > or do I need to loop through each array item one by one looking for a
match?


Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.


Thanks! I will look into ArrayLists!

-Darrel
Nov 19 '05 #7
> Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.


Hmm...it does seem like a hack, but, at the same time, seems more efficient.
It's already a text string to begin with, and with this, I'm only searching
once, instead of looping through it all.

Not that I don't need to learn arrays, of course. ;o)

-Darrel
Nov 19 '05 #8
> > or do I need to loop through each array item one by one looking for a
match?


Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.


Thanks! I will look into ArrayLists!

-Darrel
Nov 19 '05 #9
sam
Not to beat this to death, but if you plan to do this a lot you should
put them as keys into a hashtable with the value set to a dummy '1' or
something. Then you can do hashtable.ContainsKey(value).

This is done pretty much in constant time, and isn't linear to length
like the Contains() method.

Nov 19 '05 #10
> Not to beat this to death, but if you plan to do this a lot you should
put them as keys into a hashtable with the value set to a dummy '1' or
something. Then you can do hashtable.ContainsKey(value).

This is done pretty much in constant time, and isn't linear to length
like the Contains() method.


I've never used hashtables. Will have to check that out!

-Darrel
Nov 19 '05 #11
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Thanks! I will look into ArrayLists!


Do yourself a favour and check out the other members of the
System.Collections namespace e.g. SortedList, Hashtable etc - massive
improvements over the old Dictionary, Collection objects etc...
Nov 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
2
by: Tek9_AK | last post by:
I have a function which reads a csv file into an array from there I want to check if a value is in it. Basically the csv files look like this: 23123233123,name,some@one.com...
4
by: Darrel | last post by:
I'm trying to add an extra layer of error checking on a Drop Down List. The list is populated from one table, and then I select the selectedValue from another DB. While it SHOULDN'T ever happen,...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
8
by: Fuzzydave | last post by:
Okay, I have been handed a python project and working through it I have had to add a report. I am returning 10 variables the results of an SQL Query and as usual the number of results vary from...
4
by: chinu | last post by:
HI all, i am declaring an array in javascript var a = new array(); now before assigning a value to the ith element of this array, i have to check if some value has already been assigned there....
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
4
by: reinhout | last post by:
Hi everyone, I have made a script that checks if content is entered in the html boxes and if the required fields are empty, their border lights red. This all works, but if I try to make a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.