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 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
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
"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.
"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.
> 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
> > 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
> 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
> > 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
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.
> 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
"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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |